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)

21st Jul 2019, 10:36 PM
Martin Lawrence
Martin Lawrence - avatar
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/
21st Jul 2019, 10:46 PM
Jake
Jake - avatar
+ 1
Thanks man! Worked and i appreciate you taking the time and breaking it down
21st Jul 2019, 10:57 PM
Martin Lawrence
Martin Lawrence - avatar