【发布时间】:2019-08-12 08:26:45
【问题描述】:
所以我的代码有问题,我的字数总是等于“4”,每当我输入不同数量的字时,这都是不准确的。
这是我的代码:
word=raw_input("Enter your string please: ")
count=0
for i in "word":
count += 1
if word == " ":
print(count)
print "Your word count:", count
print "Your character count:", (len(word))
示例输出:
Enter your string please: ched hcdbe checbj
Your word count: 4
Your character count: 17
我的字数非常好,这只是我的字数。我很困惑我需要解决什么。非常感谢您的帮助!
【问题讨论】:
-
for i in "word"?您正在遍历字符串'word',它有 4 个字符。请参阅变量word -
哦,所以我把引号去掉了?
-
但是当我只有 3 个单词时它告诉我我有 17 个单词 :(
-
是的,没错。请注意,您没有计算单词。您必须为此拆分字符串。所以更像
len(word.split()) -
那么我该如何合并它呢?
标签: python string count word-count