Yes or No output Python [duplicate] - python

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 9 years ago.
Join = input('Would you like to join me?')
if Join == 'yes' or 'Yes':
print("Great," + myName + '!')
else:
print ("Sorry for asking...")
So this is my code. It's longer; just including the problem. I'm asking a yes or no question and when in the console it runs smoothly until you get to it. Whatever you type you get the 'yes' output. Could someone please help? I've used elif statements as well but no luck.

if Join == 'yes' or 'Yes':
This is always true. Python reads it as:
if (Join == 'yes') or 'Yes':
The second half of the or, being a non-empty string, is always true, so the whole expression is always true because anything or true is true.
You can fix this by explicitly comparing Join to both values:
if Join == 'yes' or Join == 'Yes':
But in this particular case I would suggest the best way to write it is this:
if Join.lower() == 'yes':
This way the case of what the user enters does not matter, it is converted to lowercase and tested against a lowercase value. If you intend to use the variable Join elsewhere it may be convenient to lowercase it when it is input instead:
Join = input('Would you like to join me?').lower()
if Join == 'yes': # etc.
You could also write it so that the user can enter anything that begins with y or indeed, just y:
Join = input('Would you like to join me?').lower()
if Join.startswith('y'): # etc.

I answered this question yesterday
You can use .lower()
Join = input('Would you like to join me?')
if Join.lower() == 'yes':
print("Great," + myName + '!')
else:
print ("Sorry for asking...")

That's not how the or operator works. Replace:
if Join == 'yes' or 'Yes':
with:
if Join in ['yes', 'Yes']:
and it'll do what you want.
EDIT: Or try this, for more general purpose:
if 'yes'.startswith(Join.lower()):
which ought to match 'y', 'Y', 'ye', 'Ye', 'YE', and so on.

The if statement should actually be:
if Join=='yes' or Join =='Yes'
The way the if statement is written in your code will cause code to be evaluated this way:
(if Join == 'yes') or ('Yes'):
Note that ('Yes') is a truthy and will always evaluate to true

Related

Why does my if statement command not been functioning [duplicate]

This question already has answers here:
How to check variable against 2 possible values?
(6 answers)
Closed 4 years ago.
I am trying to program an ATM machine with this code in python. But regardles of what is inputted, it just says that the card is successfully inputed.
inputCard = input("Welcome to the atm machine, please insert your credit card (Type 'Yes' when you have done so) ")
if inputCard == ['No', 'no']: #checks if card has been entered
print ("Please retry")
else:
print ("Card is successfully inputed") `
Thanks
The equality operator == compares whether the input, which is a string, equals the right hand side, which is a list. Intuitively, a list will never equal a string.
So, use the in operator to see if the answer is in the possible options:
if inputCard in ('No', 'no'):
Alternatively, convert the answer to lowercase and then use ==:
if inputCard.lower() == 'no'
This way will accept no, No, NO and nO.
You are comparing the "inputCard" to a list. Try:
if inputCard.lower() == "no":
inputCard is str,["NO","no"] is list.They will not equal.And you can try like this
if inputCard.lower() == 'no':
or
if inputCard.upper() == 'NO':

Python: Program not checking full or statement [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 5 years ago.
some assistance is needed:
I've been having some trouble with the OR operand ...I understand how it works ( either or condition must be true), but i'm not sure why it is behaving in the fashion explained below.
The 'or' statement doesn't seem to be checking the second condition in my "flowContol' function.. when I enter 'y' it sees the condition as true and runs without any problems... however when i enter 'yes' it evaluates the condition as false...
-I excluded the other functions-..
def flowControl():
answer = input("do you want run the 'displayLession' function? ( yes or
no)").strip()
if answer == ('y' or 'yes'):
displayLesson()
else:
userTime()
print('End program')
flowControl()
What you are looking for is
if answer == 'y' or answer == 'yes':
Or
if answer in ['y', 'yes']:
Why? Simple, your code evaluates before what is between parentheses 'y' or 'yes' and this is 'y', because 'y' is a truthy expression in python, so no need to evaluate the latter expression.

Python: Shorter way of checking if a string equals two other strings [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 6 years ago.
have just started learning python and this question came up to my mind
is there a shorter way to determine if a string equals 'something' or 'somethingelse'?
example:
input = raw_input("question?")
while input != 'n' and input != 'y':
#ask question again
You could check whether it is in a list or set.
input = raw_input("question?")
while input not in ['n', 'N']:
#ask question again
If you are just trying to accept two cases though, you could also just call lower on the input.
while input.lower() != 'n':
#ask question again
Perhaps unexpectedly, 'a' != input != 'b' works. It resolves the same way as ('a' != input) and ('b' != input). You can do the same thing with ==, or with <, >, etc. on numbers, as well.
Oh, but you have to be careful if you chain it longer than three things, or use multiple different comparison operators.
If it's a case issue, then you can:
while input.lower() != 'n'

why does my program keep on closing? [duplicate]

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 7 years ago.
im a newbie to python and my program keeps on closing when typing in the correct decision "y" here is my code feel free to edit my code:im new to python and not the best at using loops.My program should give the user a closing message if "n" or "N" is typed eg.press enter to exit the program and if "yes" or "y" is typed it should carry on going to ask the users name Any Help is very much appreciated:Is my Loop working properly?
play_user = input ("Do You Want To Play?")
play_user = "y" and "Y"
while play_user == "n" and "N":
play_user = input ("Do You Want To Play")
Try this instead:
while input ("Do You Want To play?").tolower[0:1] != "n":
play_game()
or
while input ("Do You Want To play?").tolower[0:1] == "y":
play_game()
A couple things first.
Instead of checking if something is equal to its upper case and lower case forms, just use the .lower() method for strings (converts string to lowercase).
But more importantly, what you are doing is setting play_user to something that's not n or N right before the while loop, so it never even enters the while loop.
I would rewrite as
play_user = raw_input("Do you want to play?\n") # note the new line here
while play_user.lower() != "y":
play_user = raw_input("Do you want to play?\n")
which will keep looping until you enter y or Y.

How can I use while loop and if statement correctly in python? [duplicate]

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 8 years ago.
I have been having trouble with my python code and have been asking around for help. I have heard mixed things about this website but i have no where else to turn so i was hoping someone on here could show me the light and see if they can find out what's wrong with my code (other then it being messy and inefficient). here it is
D=False
while D==False:
input=raw_input("please type 1 for 10p, 2 for 20p, 3 for 50p, 4 for 1 pound")
while input not in ['1', '2', '3', '4']:
print "That is not a correct coin value"
input = raw_input("input: ")
else:
if input=="1":
m=m+10
if input=="2":
m=m+20
if input=="3":
m=m+50
if input=="4":
m=m+100
print("your current credit in pennys is:")
print(m)
D2=raw_input("are you done")
if D2=="yes" or "Yes":
D=True
else:
D=False
I kind of mucked up the implantation of the code on here but you get the picture. It all works fine until you get to the bit asking you if you are done. For some reason it carries on even if you don't put yes. Can any nice coders out there tell me what's up with it
This
if D2=="yes" or "Yes":
always evaluates to true, as it's essentially parsed like this:
if (D2=="yes") or "Yes":
You probably wanted:
if D2 == "yes" or D2 == "Yes":
Which is better written as:
if D2.lower() == "yes":
Or:
if D2 in ["yes", "Yes"]:

Categories