IN PYTHON3... why am I still getting an infinite loop here if I type something o
ID: 3730569 • Letter: I
Question
IN PYTHON3... why am I still getting an infinite loop here if I type something other than yes or no?
ans1 = input("Do you want to replace phrases?: ")
ans1 = ans1.upper()
v = 1
while v == 1:
if ans1 == "yes".upper():
acro_string = acronym(main_string)
v = 2
elif ans1 == "no".upper():
acro_string = main_string
v = 2
else:
print("Please answer yes or no. Do you want to replace phrases?: ")
v = 1
Explanation / Answer
Because if you enter anythng other than yes and no, then the else block is executed and the variable v is set to 1. The condition for while loop to terminate is that v has value other than 1. So, it runs in an infinite loop as you are not asing the user to enter ans1 inside he loop, So, the else block is executed always and the loop runs infinitely.
Correct Code
v = 1
while v == 1:
ans1 = input("Do you want to replace phrases?: ")
ans1 = ans1.upper()
if ans1 == "yes".upper():
acro_string = acronym(main_string)
v = 2
elif ans1 == "no".upper():
acro_string = main_string
v = 2
else:
print("Please answer yes or no. Do you want to replace phrases?: ")
v = 1