为什么我的while循环在我添加休息后继续要求输入?

时间:2017-09-10 00:59:49

标签: python-3.x loops while-loop

我有一个类的作业,用于创建一段允许用户输入单词的代码,它将返回包含' i'或者是' a。但是,如果用户输入"退出",程序将停止。我只是不断要求输入,即使休息应该已经停止了。

wordprintA = [0]
wordprintI = [0]
dontprint = [0]
while True:
wordlist = [input("Enter a word:").upper()]

for char in wordlist:
    if char == 'A':
       wordprintA.append(wordlist)

for char in wordlist:
    if char == 'I':
       wordprintI.append(wordlist)

else:
      dontprint.append(wordlist)

for char in wordlist:
    if char == 'EXIT':
       break
print(wordprintA, wordprintI)

任何见解都会有所帮助。

1 个答案:

答案 0 :(得分:2)

中断让你退出for循环而不是while循环。您需要更改while循环以检查绑定到char出口的变量或者第二次检出for循环。见下面的例子

check=true
while check==true:
   wordlist = [input("Enter a word:").upper()]

   for char in wordlist:
      if char == 'A':
         wordprintA.append(wordlist)

   for char in wordlist:
      if char == 'I':
         wordprintI.append(wordlist)

      else:
         dontprint.append(wordlist)

   for char in wordlist:
      if char == 'EXIT':
         check=false
         break
   print(wordprintA, wordprintI)