0
I need help!!!
HI i'm a beginner at python and I wrote a simple program where you can choose from a list I created containing noun's,verb's or adjectives. When I go to loop it I get a infinite loop. Can someone show me the most practical way to have it move to the next loop? while answer == 'a': print(adjective) while answer == 'n': print(noun) while answer == 'c': print(city)
2 Answers
+ 1
while loops continue to loop the block of code within its scope as long as the continuation condition returns true. Each of the continuation conditions in your while loops will always be true if the value of answer does not change, thus they will never stop executing/looping. Lets look at your first while loop as an example:
if you have that variable: answer = input()
and the user sets that variable to âaâ now the variable âanswerâ will ALWAYS be set to the value of âaâ unless changed.
so your while loop is saying:
While answer has the value of âaâ then execute the code within my scope.
See the issue? you havent specified that answer no longer equals a, thus that condition will always be true and the loop will never stop. For your purpse, an if-elif structure would be the suitable route:
if answer == âaâ:
print(adjective)
elif answer == ânâ:
print(noun)
elif answer == âcâ:
print(city)
https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2281/
https://www.sololearn.com/learn/Python/2277/
+ 1
Thanks man! Worked and i appreciate you taking the time and breaking it down