This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 5 years ago.
Hello programmers of StackOverFlow, I am a teenager that studies Python, And I can't find the logical errors in one of my codes. I wish you could help with that.
I got two logical errors.
1. If the answer is "yes" or "no" it keeps opening the while commands while it shouldn't.
2. Concluding from first error it never stops when the choice == "yes" or "no"..
My coding is here:
choice = raw_input("Do you like programming? (yes/no) : ")
while choice != "yes" or "no":
choice = raw_input('Come on, answer with a "yes" or with a "no" ! : ')
if choice == "yes":
print "I like you !"
elif choice == "no":
print "I am sorry to hear that .. "
, Thank you in advance !
the second line evaluates to True. Because the string "no" evaluates to true
try the following
if "any string": print(1) #will always print 1, unless the string is empty
What you should use instead is
while choice not in ["yes", "no"]:
which checks if choice matches either "yes" or "no"
This is the problem:
while choice != "yes" or "no":
This is interpreted by Python as (choice != "yes") or ("no"). Since "no" is a string of non-zero length, it is truthy. This makes the or expression true because anything OR true is true. So your condition is always true and the loop never stops.
It should be:
while choice != "yes" and choice != "no":
Or:
while choice not in ("yes", "no"):
Related
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Closed 4 months ago.
I don't know how exactly to word it so ill just explain,
elif careful != "yes" "no":
How do i make the "yes" and "no" independent? When i put it in, it combines the "yes" and "no" together.
Output 👇
'yesno'
I have tried using the '|' operator and the '&' operator
Here is the full code 👇
careful = input("Are you Careful?")
if careful == "yes":
print("ok good, what is your age?")
age = int(input("Your age?"))
if age <= 13:
print("sorry, paws is hiding")
elif careful != "yes" "no":
print("nonsense, say yes or no")
else:
print("Be careful! she nibbles, with no teeth")
else:
print("you have to be careful!")
thanks
Instead of,
elif careful != "yes" "no":
You can use
elif careful not in ("yes" , "no"):
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 5 years ago.
So I coded a function that takes the value of a batteries variable. I want it to do something if the value is "yes" or "no". If the value is none of these answers, I want it to to ask it again for an indefinite amount of times. Here is my code in case my description was bad.
def batterie_answer():
batteries = raw_input("yes or no > ").lower()
print batteries
while True:
if batteries != "yes" or batteries != "no":
print "Please respond yes or no"
raw_input("> ")
continue
elif batteries == "yes":
print "batteries taken!"
items["batteries"] = 1
break
elif batteries == "no":
print "Probably a wise choice. Save some space!"
break
batterie_answer()
You need to move the assignment to inside the while loop or add another assignment. You also need to change the or to an and. You would also need to remove the extra raw_input line that does not assign the value to the batteries variable.
def batterie_answer():
while True:
batteries = raw_input("yes or no > ").lower()
print batteries
if batteries != "yes" and batteries != "no":
print "Please respond yes or no"
continue
elif batteries == "yes":
print "batteries taken!"
items["batteries"] = 1
break
elif batteries == "no":
print "Probably a wise choice. Save some space!"
break
batterie_answer()
The problem is in your if statement. It doesn't matter what value is given, the condition will always equate to true and it will ask again for a value. For example, if the value was "yes" then the condition would be (false or true) which equates to true, if the value was "no" then it would be (true or false) which also equates to true, if the value was something like "asdf" then it would be (true or true) which also equates to true.
to fix this change the "or" operator to an "and" operator.
if batteries != "yes" and batteries != "no":
Your while loop doesn't include the original raw_input() command, so it's never going to ask for input after the first iteration. Instead, it will only ever print out the answer over and over again.
Move the while True up a few lines so that it encompasses the raw_input() command.
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 7 years ago.
So, I Am trying to create some code, and for whatever reason the following always gives the same output, "Great! let's continue"
I am fairly new programmer, so any explanation has about why this is happening and how I can fix it is welcome. Thank you!
#python 2
UserAnswer = raw_input('Is your age and birthday correct? (yes/no):')
if UserAnswer == 'yes' or "Yes":
print ("Great! let's continue.")
elif UserAnswer == 'no' or "No":
print("check for your error, the program will now end")
SystemExit
else:
print 'Invalid input.'
The following line will always evaluate to True:
if UserAnswer == 'yes' or "Yes"
This is because "Yes", when treated as a boolean, evaluates to True. So the result of the OR will be True. You can fix it by changing it to the following (and do the same thing for the other line)
if UserAnswer == 'yes' or UserAnswer == 'Yes'
Don't do:
if UserAnswer == 'yes' or "Yes":
Do instead:
if UserAnswer == 'yes' or UserAnswer == "Yes":
Same applies to the elif condition
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 8 years ago.
When I type no into the input on the terminal. It goes through the if choice == "yes" part.
I want it to go through the else. Please help.
choice=raw_input("Will you help us? Yes or no?")
if choice == "yes" or "Yes":
print "Yeah! You are a hero!"
name = raw_input("What is your name?")
print "Alright, " + str(name) + " ,let's go choose a weapon from the blacksmith."
else:
print "You're a coward. :("
quit()
What's wrong?
The bug is in this line of code:
if choice == "yes" or "Yes":
Python sees this as an "or" of two conditions:
if (choice == "yes") or ("Yes"):
Which is same as:
if (choice == "yes") or True:
because a non-empty string is always True.
And this finally reduces to:
if True:
as "or"ing with True always evaluates to True.
This would give you the desired result:
if choice == "yes" or choice == "Yes":
However, that is considered C-style and the pythonic way of comparing multiple values is:
if choice in ("yes", "Yes"):
But in this case, you just want to do a case-insensitive match. So the right way to do that would be:
if choice.lower() == "yes":
And this would even handle odd capitalization in inputs like "yEs" or "YEs".
choice=raw_input("Will you help us? Yes or no?")
if choice == "yes" or choice == "Yes":
print "Yeah! You are a hero!"
name = raw_input("What is your name?")
print "Alright, " + str(name) + " ,let's go choose a weapon from the blacksmith."
else:
print "You're a coward. :("
quit()
The above is the correct format. You did not have the logic set up correctly. Note the following:
a = 1
if a == 2 or 3 :
print 'OK'
It prints 'OK'. Why?
The reason is that python values are evaluated in a left to right fashion. If any value is true then that value is returned. However if all values are false then the last value is returned, in your case 'Yes'. This is what is causing you problems as far as I understand it. You need basically two 'or' conditions, not one....
My code is:
def nameAndConfirm():
global name,confirm
print("What is your name? ")
name = input()
str(name)
print("Is",name,"correct? ")
confirm = input()
str(confirm)
print(confirm)
if confirm.upper() == "Y" or "YES":
classSelection()
elif confirm.upper() == "N" or "NO":
nameAndConfirm()
else:
print("Valid answers are Y/Yes or N/No!")
nameAndConfirm()
nameAndConfirm()
Critique on this code would be nice as well. I know its very shifty, I know how to make it shorter in some ways but I was trying to get my if-elif-else to work. I have no clue what else I can do as I've tried everything I know. Also I made an indent 4 spaces in the above code. **Edit: sorry the error is that it always runs the "if" it never goes past the first if line no matter what you enter in for confirm
The condition confirm.upper() == "Y" or "YES" and the other one are not evaluated as you expect. You want
confirm.upper() in {"Y", "YES"}
or
confirm.upper() == "Y" or confirm.upper() == "YES"
Your condition is equivalent to:
(confirm.upper() == "Y") or "YES"
which is always truthy:
In [1]: True or "Yes"
Out[1]: True
In [2]: False or "Yes"
Out[2]: 'Yes'
On a separate note, the lines
str(name)
and
str(confirm)
don't do anything. The values returned by the functions are not saved anywhere, and name and confirm are not altered. Moreover, they are already strings to begin with, because they hold the return values of input().