I have recently just finished up some code of a very basic troubleshooting system. I'm just trying to add some validation so that if the user inputs anything which isn't yes or no on any of the questions then it will print 'Invalid input' in the shell but just not sure how to do it exactly. Can someone help me, please? Much appreciated.
My code:
prompt = "> "
print ("screen question one here")
screen = input(prompt)
if screen == "yes":
print ("screen question two here")
screen2 = input(prompt)
if screen2 == "yes":
print ("screen question three here")
screen3 = input(prompt)
if screen3 == "yes":
print ("screen advice one here")
elif screen3 == "no":
print ("screen adivce two here")
elif screen2 == "no":
print ("camera question one here")
camera = input(prompt)
if camera == "yes":
print ("camera question two here")
camera2 = input(prompt)
if camera2 == "yes":
print ("camera advice one here")
elif camera2 == "no":
print ("camera advice two here")
elif camera == "no":
print ("water question one here")
water = input(prompt)
if water == "yes":
print ("water question two here")
water2 = input(prompt)
if water2 == "yes":
print ("water advice one here")
elif water2 == "no":
print ("water advice two here")
elif water == "no":
print ("buttons question one here")
buttons = input(prompt)
if buttons == "yes":
print ("buttons advice one here")
elif buttons == "no":
print ("buttons advice two here")
elif screen == "no":
print ("battery question one here")
battery = input(prompt)
if battery == "yes":
print ("battery question two here")
battery2 = input(prompt)
if battery2 == "yes":
print ("battery advice one here")
elif battery2 == "no":
print ("battery advice two here")
elif battery == "no":
print ("wifi question one here")
wifi = input(prompt)
if wifi == "yes":
print ("wifi advice one here")
elif wifi == "no":
print ("wifi advice two here")
Here is one way you might do it.
Define a function that gets a yes or no from the user. What people tend to want is to repeat the question until the user gives you a suitable response: that is what this function does.
def yesorno(question):
while True:
print(question)
answer = input('> ').strip().lower()
if answer in ('y', 'yes'):
return True
if answer in ('n', 'no'):
return False
print("Invalid input")
On the other hand, if you just want to exit your script when you get invalid input, you could do this:
def yesorno(question):
print(question)
answer = input('> ').strip().lower()
if answer in ('y', 'yes'):
return True
if answer in ('n', 'no'):
return False
exit('Invalid input')
exit will simply exit your whole program. It's not necessarily what I would recommend.
Either way, you can use yesorno like this:
if yesorno("Question 1"):
# User answered yes
if yesorno("Question 2A"):
# User answered yes
else:
# User answered no
else:
# User answered no
if yesorno("Question 2B"):
...
Related
i have this problem:
def thing():
e = input("Are you sure you want to enter this menu?")
if e == "yes":
while True:
placeholder()
elif e == "no":
return menu() #<-- THIS
else:
print("invalid response")
def menu():
print("""
what do you want to do?
1. Enter thing
2. Don't enter thing
""")
ans = input("Enter your response here:\n")
if ans == "1":
thing()
elif ans == "2":
pass
else:
print("invalid response")
If e == no then it won't go to menu() like i expected but it just stopped and i need a way to make what happens after they input "no" to be menu() yet it will not do that. any way to fix?
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'
...
I dont know why I have to input q n times of question generated to quit the program, what should I do so that the program closes instantly when I input q only once?
greetings = input("Hello, what should i call you? ")
def generate_again_or_quit():
while True:
option = input("Press any key to generate another question or Q to exit" ).lower()
if option == "q":
break
generate_questions()
def generate_questions():
print(random_questions_dict.get((random.randint(1, 30))))
generate_again_or_quit()
while True:
greetings2 = input("Do you want me to generate some questions "+greetings+"?").lower()
if greetings2 == "yes":
generate_questions()
break
elif greetings2 == "no":
print("See you later...")
break
else:
print("Please answer with yes or no")
continue
As the comments suggest, pick iteration or recursion. Here's a recursion example for your case
import random
random_questions_dict = {1: "why", 2: "what", 3: "when", 4: "which", 5: "how"}
def generate_questions():
print(random_questions_dict.get((random.randint(1, 5))))
option = input("Press any key to generate another question or Q to exit" ).lower()
if option == "q":
return
generate_questions()
greetings = input("Hello, what should i call you? ")
while True:
greetings2 = input("Do you want me to generate some questions "+greetings+"?").lower()
if greetings2 == "yes":
generate_questions()
break
elif greetings2 == "no":
print("See you later...")
break
else:
print("Please answer with yes or no")
continue
So I'm trying to figure out how I can make this simple little program to go back to the raw_input if the user inputs something else then "yes" or "no".
a = raw_input("test: ")
while True:
if a == "yes":
print("yeyeye")
break
elif a == "no":
print("nonono")
break
else:
print("yes or no idiot")
This is what I got so far, I'm new and it's hard to understand. Thanks in advance.
As #DavidG mentioned, just add your raw_input statement in loop:
while True:
a = raw_input("Enter: ")
if a == "yes":
print("You have entered Yes")
break
elif a == "no":
print("You have entered No")
break
else:
print("yes or no idiot")
Simply you can put the first instruction inside the loop; in this way, every time the user inserts a value different to yes or no you can print a message and wait to a new input.
while True:
a = raw_input("test: ")
if a == "yes":
print("yeyeye")
break
elif a == "no":
print("nonono")
break
else:
print("yes or no idiot")
Describe a condition checker for while and read input everytime when your condition is not meet. Inline returns are good for low quantity conditions but when your choice count is too much or condition in condition situations appear, inline returns are becoming trouble.
Thats why you must use condition checkers(like cloop) instead of inline returns.
cloop=True
while cloop:
a = raw_input("test: ")
if a == "yes":
print("yeyeye")
cloop=False
elif a == "no":
print("nonono")
cloop=False
else:
print("yes or no idiot")
cloop=True
from sys import exit
def answer():
answer = raw_input("> ")
if answer == "Yes" or answer == "yes":
#going to next
joint()
elif answer == "No" or answer == "no":
print "You still have something, I know..."
again()
else:
fubar()
def again():
again = raw_input("> ")
if again == "Yes" or again == "yes":
#going to next
joint()
elif again == "No" or again == "no":
print "You still have something, I know..."
else:
fubar()
def fuck():
print "Fubar'd!"
def joint():
print "To be continue..."
def question():
print "Hi duuuude..."
raw_input("To say 'Hi' press Enter")
print "Can you help me?"
answer()
question()
Hi, can you help me with this? I`m trying to repeat the function "answer", when I get answer "NO". Im want to escape function "again"... And also is there a way to escape "answer == "Yes" or answer == "yes": " so no matter I write capital or small letter to accept the answer and not to write like a noob "Yes" or "yes"?
This is usually achieved with a while loop.
Edit: As pointed out, while loops are nice and clear, and avoid recursion limits.
Never thought a simple answer would generate so many votes....
Lets give you an example
while True:
ans = raw_input("Enter only y or n to continue").strip().lower()
if ans == "y":
print "Done!"
break
elif ans == "n":
print "No?"
else:
print "Not valid input."
The simplest solution to your problem is remove your again function, and recurse:
def answer():
ans = raw_input("> ")
if ans == "Yes" or ans == "yes":
#going to next
joint()
elif ans == "No" or ans == "no":
print "You still have something, I know..."
answer() # again()
else:
fubar()
I had to rename your answer variable to ans so that it didn't clash with the function name.
For the second question, you want either:
if answer.lower() == "yes":
or
if answer in ("Yes", "yes"):