Reputation: 47
I have an assignment for a class to create a piece of code that will allow the user to input words and it will return the words that contain an 'i' or an 'a'. However, if the user inputs "Exit", the program will stop. Mine just keeps asking for input, even though the break should have stopped it.
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)
Any insight would be helpful.
Upvotes: 1
Views: 419
Reputation: 2826
The break is taking you out of the for loop not the while loop. You either need to change your while loop to check a variable tied to the char exit or have a second check out of the for loop. See the below example
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)
Upvotes: 2