这是我到目前为止有:的Python:如何使用while循环和输出正确的字数
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'? – 2012-02-23 05:39:57
我把它放在while循环之前。 wordCount = len(文字) – user1227404 2012-02-23 05:41:47
请确保您发布生成您的输出的实际代码。现在,你的代码会失败,因为在测试长度之前未定义“单词”。 – 2012-02-23 05:59:56