choice1=("yes")
choice=("no")
import sys
while True:
try:
user5=input("is your phone working")
except ValueError:
print("Sorry, I didn't understand that, make sure to reply with 'YES' or 'NO'")
if user5==choice1:
print("thats great")
sys.exit(0)
break
elif user5==choice:
print("you could contact seller")
while True:
try:
user_response=input("is it fix?")
except ValueError:
print("sorry i didnt understand that, make sure to reply with 'YES' or 'NO'")
if user_response==choice1:
print("great")
sys.exit(0)
break
elif user_response==choice:
break
break
When the user gives an invalid response, its suppose to print the error message but it skips it, and repeats the question. Like so:
is your phone workinglll
is your phone working;';'';
is your phone workinglll;l;l
is your phone working;ll;';';l;';'
is your phone working
When the user enters an invalid response, its suppose to display the message
:sorry I didn't understand that, make sure to reply with 'YES' or 'NO and then ask the question again until it gets a valid response.
解决方案input just accepts any string, it won't throw a ValueError if the response is anything else than yes or no.
Instead, you have to use an if-else statement that checks that you responded yes or no, and if you didn't, the continue statement returns to the beginning of the loop so you get the question again.
user5 = input("Is your phone working?").lower()
if user5 != choice and user5 != choice1:
print("Sorry, I didn't understand that, make sure to reply with 'YES' or 'NO'.")
continue
And:
user_response = input("Is it fixed?").lower()
if user_response != choice and user_response != choice1:
print("Sorry, I didn't understand that, make sure to reply with 'YES' or 'NO'.")
continue