【发布时间】:2016-10-10 21:58:01
【问题描述】:
这是我目前所拥有的:
while len(words) != 5:
words = raw_input("Enter a 5 worded sentence: ").split()
print "Try again. The word count is:", wordCount
if len(words) == 5:
print "Good! The word count is 5!"
问题是我明白了:
Enter a 5 worded sentence: d d d d
Try again. The word count is: 4
Enter a 5 worded sentence: d d d d d d
Try again. The word count is: 4
Enter a 5 worded sentence: d d d d d
Try again. The word count is: 4
Good! The word count is 5!
当我输入多于或少于 5 个字时,它会保持字数不变。
【问题讨论】:
-
你在哪里初始化变量
wordCount? -
我把它放在while循环之前。 wordCount=len(words)
-
确保发布生成输出的实际代码。照原样,您的代码将失败,因为在测试长度之前未定义
words。
标签: python while-loop python-2.7 word-count