This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 6 years ago.
print_list=input("Do you wish to print list \n:")
if print_list == "yes":
for item in List:
print (item , "%2.f" %(Speed),"m/s")
elif print_list == "no":
print ("Thank you")
if print_list != "yes" or "no":
while True:
print ("Invalid Input")
break
this is what happened:
Do you wish to print list
:hhh
Invalid Input
Press "Enter Key" when the vehicle passes Sensor 1
:
What I was hoping was for the program to ask the question "Do you wish to print list", when the user input is invalid.
If you want the first statement to happen again then it has to be inside the loop. Basically all of this needs to be inside the while loop. And the break will cause the loop to end, so definitely don't put it after invalid input since you want the user to be asked again.
while True:
print_list=input("Do you wish to print list \n:")
if print_list == "yes":
for item in List:
print (item , "%2.f" %(Speed),"m/s")
break
elif print_list == "no":
print ("Thank you")
break
else:
print ("Invalid Input\n")
Related
This question already has answers here:
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Closed 3 months ago.
I was using a yes/no loop to make an infinite loop which would end when user enters no or No but the program was not working properly. I know the what the error is but i don't know why is it occuring like this. Can anyone tell how to fix the error without changing my initial program
when i use this code it works but when i use if a=='yes' or 'Yes' and elif a=='no' or 'No' in the somehow the output shows the print statement of the if statement even when i enter no.
My program without the OR condition
while True:
a = input("Enter yes/no to continue")
if a=='yes':
print("enter the program")
elif a=='no':
print("EXIT")
break
else:
print("Enter either yes/no")
My initial program with OR condition
while True:
a = input("Enter yes/no to continue")
if a=='yes' or 'Yes':
print("enter the program")
elif a=='no' or 'No':
print("EXIT")
break
else:
print("Enter either yes/no")
In an or statement you have to compare a with the value in all expressions:
while True:
a = input("Enter yes/no to continue")
if a == 'yes' or a == 'Yes':
print("enter the program")
elif a == 'no' or a == 'No':
print("EXIT")
break
else:
print("Enter either yes/no")
A more pythonic way is to use .lower() in your case. For example:
a == 'yes' or a == 'Yes' # is equeal to:
a.lower() == 'yes'
You have a few options:
while True:
a = input("Enter yes/no to continue")
if a.lower()=='yes':
print("enter the program")
elif a.lower()=='no':
print("EXIT")
break
else:
print("Enter either yes/no")
or you can do this:
while True:
a = input("Enter yes/no to continue")
if a=='yes' or a=='Yes':
print("enter the program")
elif a=='no' or a=='No':
print("EXIT")
break
else:
print("Enter either yes/no")
When you use or, you should write complete condition again.
Here if you want to check a=="Yes" also, you should declare it completely.
if a == 'yes' or a == 'Yes':
...
You can also use this:
if a.lower() == 'yes'
...
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 11 months ago.
I'm coding a big project that has many functions. I want my code to restart functions where errors occur due to user input, to illustrate what I mean I have made this short code:
def myfunction():
UserInput = int(input("Enter 1 or 2"))
if UserInput == 1:
print("hello")
elif UserInput == 2:
print("Bye")
else:
print("Error!, Please choose 1 or 2")
#I want it to restart myfunction()
thanks.
As #ti7 commented you better use a while loop and not call your function again on error (recursion)
I don't know what you want to do with the input but as I see you do not need to convert it to an integer to check if it's 1 or 2. Because you can get a ValueError if the input is not convertible to integer.
What you'll do, get the input, use a while loop and check if the input is not "1" and "2"
def myfunction():
UserInput = input("Enter 1 or 2")
while UserInput != "1" and UserInput != "2":
UserInput = input("Enter 1 or 2")
if UserInput == "1":
print("hello")
elif UserInput == "2":
print("Bye")
else:
print("Can't be else")
Simple, just call the function. (You will want the extra UserInput so you can read the text)
def myfunction():
UserInput = int(input("Enter 1 or 2"))
if UserInput == 1:
print("hello")
elif UserInput == 2:
print("Bye")
else:
print("Error!, Please choose 1 or 2")
UserInput = input('Press anything to continue. ')
myfunction()
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 3 years ago.
I would like for when a player says no for it to say goodbye then end but instead it goes to Choose a category. Any suggestions?
while True:
choice = input ("Do you want to play?")
if choice == "yes":
print("Great!")
break # Go to the game
elif choice == "no":
print("Goodbye.")
break # nothing
else:
print("Please answer yes or no.")
continent = input("Choose a category: \n"
"Africa \n"
"Asia \n"
"Antarctica \n"
"Australia \n"
"Europe \n"
"North America \n"
"South America \n")
This question is different because its not asking about the loop it is asking how to properly end the program.
while True:
choice = input ("Do you want to play?")
if choice == "yes"
print("Great!")
break # Go to the game
elif choice == "no"
print("Goodbye.")
break # nothing
else:
print("Please answer yes or no.")
This question already has answers here:
how to stop a for loop
(9 answers)
Closed 6 years ago.
I am making a script game and use this code for multiple choice.
while True:
d1a = input ("Do you want to: A) Befriend Fred. B) Be mean to Fred. [A/B]? : ")
if d1a == "A":
print ("You befriend fred. You now have a friend..")
elif d1a == "B":
print ("You angered Fred. He kills you. RIP.")
elif d1a == "Q" :
break
After this happens,If you type A or B it will print the desired text and then ask you again "Do you want to befriend fred?" etc.
You can simply add break statements to each conditional:
while True:
d1a = input ("Do you want to: A) Befriend Fred. B) Be mean to Fred. [A/B]? : ")
if d1a == "A":
print ("You befriend fred. You now have a friend..")
break
elif d1a == "B":
print ("You angered Fred. He kills you. RIP.")
break
elif d1a == "Q" :
break
Also if you want to leave the loop after one iteration the loop isn't necessary. However, I assume you're looping until you get the correct input, in which case it might be nice to tell your player that the input was incorrect:
while True:
d1a = input ("Do you want to: A) Befriend Fred. B) Be mean to Fred. [A/B]? : ")
if d1a == "A":
print ("You befriend fred. You now have a friend..")
break
elif d1a == "B":
print ("You angered Fred. He kills you. RIP.")
break
else:
print("Incorrect input")
I'm currently working on a python project and I need it to accept random input from users at some point.
So, if I have, for example:
def question_one () :
answer_one = raw_input ('How many days are there in a week? ').lower()
try:
if answer_one == 'seven' or answer_one == '7' :
question_2()
Everything works wonders. But how can I make python accept random input, as in
def question_two () :
answer_two = raw_input ('What´s your mother´s name? ').lower()
try:
if answer_two == ***I have no idea how to code this part*** :
question_3()
In this case, I would need python to accept any input and still take the user to the next question. How could I do it?
Just remove the if clause then.
If the input doesn't have to be of a specific form, or have some particular property, then there's no need for the if statement, or even the try.
def question_two():
answer_two = raw_input("What's your mother's name?").lower()
question_3()
If you want to be able to re-ask the question, if they don't get it right then you can loop back to it like so. The only acceptable answer for this question is "yes", or "YES", etc..
If they don't answer correctly it will ask them again,till they get it right.
def question1():
answer1 = raw_input("Do you like chickens?")
answer1 = answer1.lower()
if answer1 == 'yes':
print "That is Correct!"
question2()
else:
question1()
If you want them to be able to go on to the next question even if they get it wrong, you can do like so:
def question1():
answer1 = raw_input("Do you like chickens?")
answer1 = answer1.lower()
if answer1 == 'yes':
print "That is Correct!"
else:
print "Next question coming!"
question2()
def question2():
answer2 = raw_input("How many days in a week?")
answer2 = answer2.lower()
if answer2 == '7' or answer2 == "seven":
print "That is Correct!"
else:
print "Sorry,that was wrong"
question3()