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"):
Related
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"):
...
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
This loop keeps looping even if I enter "no" and when I type "jdlfjap", for example, it continues to loop without a "?".
Does anyone know why this is?
def makeContact():
contactName = input("Name: ")
contactNumber = input("Number: ")
dictionaryForContacts[contactName] = contactNumber
def continueMaking():
while True:
continueMaking = input("\nWould you like to continue making contacts? ")
if continueMaking == "Yes" or "yes" or "YES":
makeContact()
continue
elif continueMaking == "No" or "no" or "NO":
break
else:
print ("?")
continue
The statement if continueMaking == "Yes" or "yes" or "YES": is equivalent to (continueMaking == "Yes") or "yes" or "YES": which, regardless of the value of continueMaking returns the string "YES", which is truthy and thus the makeContact call always executes. Case-insensitive string comparisons can be accomplished by continueMaking.lower() == "yes".
Overwriting the function continueMaking with the variable continueMaking adds confusion. Choose a different variable name. Readability counts.
Could anyone help me with looping this code back to the beginning if the user inputs yes and ending the program if the user inputs no?
while True:
print ("Hello, this is a program to check if a word is a palindrome or not.")
word = input("Enter a word: ")
word = word.casefold()
revword = reversed(word)
if list(word) == list(revword):
print ("The word" ,word, "is a palindrome.")
else:
print("The word" ,word, "is not a palindrome.")
print ("Would you like to check another word?")
ans = input()
if ans == ("Yes") :
#here to return to beginning
else ans == ("No"):
print ("Goodbye")
Use continue to continue your loop and break to exit it.
if ans == ("Yes") :
continue
else ans == ("No"):
print ("Goodbye")
break
Or, you could just leave off the if ans == ("Yes") and just have this:
else ans == ("No"):
print ("Goodbye")
break
Or even better, you could change your while loop to check the ans variable instead of doing while True:
Change while True to while ans == "Yes". Before the loop, you have to define ans as "Yes" (ans = "Yes). That way, it will automatically run once, but will prompt the user to continue.
Alternatively, you could do this
ans = input()
if ans == "No":
break
I can't figure out why the following doesn't work.
# Example
print "So, yes or no?"
answer = raw_input()
print answer
if answer == "Yes":
print "Yes indeed!"
elif answer == "yes" or "y":
print "Oh yeah!"
elif answer == "No":
print "No it isn't!"
elif answer == "no" or "n":
print "Not really!"
else:
print "Say what?"
When I remove the or, it does work. What am I doing wrong?
-edit-
I have it now, thanks a lot!
# Example
print "So, yes or no?"
answer = raw_input()
print answer
if answer in "Yes":
print "Yes indeed!"
elif answer in ("yes", "y"):
print "Oh yeah!"
elif answer in "No":
print "No it isn't!"
elif answer in ("no", "n"):
print "Not really!"
else:
print "Say what?"
One problem I see is this:
elif answer == "yes" or "y":
that literally translates to "if answer is yes or True" which always result in True.
You could write it like this:
elif answer in ("yes", "y"):
elif answer == "yes" or answer == "y":
elif answer == "No" or answer == "n":
Using elif answer == "yes" or "y": etc.. you are basically checking if bool("n") not comparing if answer is equal to "n" so your statements will always evaluate to True:
any non empty string will always evaluate to True:
In [38]: bool("n")
Out[38]: True
In [39]: bool("y")
Out[39]: True
In [40]: bool("")
Out[40]: False
You can also test for membership using in if you want to check against more than one value:
elif answer in {"yes", "y"}:
Change this:
answer == "Yes" or "y"
to:
answer == "Yes" or answer == "y"
or even:
answer in ("Yes", "y")