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.
Related
This question already has answers here:
Why does non-equality check of one variable against many values always return true?
(3 answers)
How to test multiple variables for equality against a single value?
(31 answers)
Closed 3 years ago.
My while loop just keeps on going even though I input y, yes, n, or no.
import random
def rollDice():
diceRoll = random.randint(1,6)
return diceRoll
def reRollDie1():
reRollDie1 = input("Would you like to re roll die 1?")
reRollDie1.lower()
while reRollDie1 != "yes" or reRollDie1 != "y" or reRollDie1 != "no" or reRollDie1 != "n":
reRollDie1 = input("Sorry that answer is invalid please try again. Would you like to re-roll die 1? ")
reRollDie1()
OUTPUT:
Sorry that answer is invalid please try again. Would you like to re-roll die 1? no
Sorry that answer is invalid please try again. Would you like to re-roll die 1? yes
Sorry that answer is invalid please try again. Would you like to re-roll die 1? yes
You're checking all the options with or. Since each is an inequality test, and each option is different from the others, the condition will always be true. I would instead suggest a test such as reRollDie1 not in {"yes", "y", "no", "n"}.
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':
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 5 years ago.
# Battle Loop
while Battle == 1:
# Taking users action to decide what to do
UserAction = input("Attack, defend, run: ")
# If the User decides to Attack
if UserAction == "Attack" and "attack":
print("Attacking!")
When the user enters "Attack" the message will be printed however if the user enters "attack" if will keep asking for UserAction instead of printing the message. How do I allow for the IF statement to accept two or more alternative strings?
You could convert the value to lower case and then you only have one comparison to make.
if UserAction.lower() == "attack":
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"]:
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 9 years ago.
I Am Not A Pro, I Have Not Been Programing For Long But Why Doesn't This Work?
door = input("I Have Found A Haunted House,Should I Go In?")
if door == "yes" or "Yes" or "YES":
print("Ok! I Am Going In")
else:
print("What?")
while door == "no" or "No" or "NO":
print("Awwh Thats A Shame, I Was Getting Excited")
Don't worry, this is a very common mistake.
You need to use in here:
if door in ("yes", "Yes", "YES"):
Or, even better, str.lower:
if door.lower() == "yes":
The reason for this is that Python evaluates non-empty strings as being True. So, your code is actually being interpreted like this:
if (door == "yes") or ("Yes") or ("YES"):
# True/False True True
As you can see, this if-statement will always pass.