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 1 year ago.
For my studies, I have begun to learn the Python Programming Language.
Fairly new to programming and having some trouble understanding why this won't work.
while compReady == False:
compAI = input("Which strategy for the computer [1,2,3]? ")
if compAI == "1" or "2" or "3":
compReady = True
elif compAI != "1" or "2" or "3":
print("Please enter either 1, 2, or 3.")
The problem I have is that no matter what is inputted into compAI, it runs through the 'if' statement. Any help is appreciated, thanks.
if condition should be written as ..
if compAI == ("1" or "2" or "3"):
In your code, Python interpreter will treat "2" and "3" as true. Please note it is not compared with compAI as per if statement syntax.
Related
This question already has answers here:
Why doesn't this "is not" if statement work?
(2 answers)
How to test multiple variables for equality against a single value?
(31 answers)
Closed 3 years ago.
I have this code:
choose = ""
while choose != "x" or choose != "X" or choose != "o" or choose != "O":
choose = input("X or O? -> ")
but it continues even if the user insert x, X, o or O.
I am new at coding, anyone have an idea to make it work?
Try this
while choose not in [ "x" , "X" , "o" ,"O"]:
Any of 4 condition becoming true continues the loop. You can use 'and' insted of 'or'.
Instead, try using 'in', 'not in' keywords of Python.
the logic combination is not what you want.
[enter image description here][1]
if you input O for choose, the condition choose != "x" is true, then while loops.
I guess what you want is:
choose = ""
while not(choose == "x" or choose == "X" or choose == "o" or choose == "O"):
choose = input("X or O? -> ")
it works~/:D
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)
How to test multiple variables for equality against a single value?
(31 answers)
Closed 5 years ago.
I'm asking input from the user and wanted to check if he used the right letters to continue, I've wrote this code, but even when I write "y" or "n", it doesn't exit the loop, and I have no idea why:
while True:
start = input("Want to start? (Y/n) ").lower()
if start != 'y' or 'n':
print("letter not accepted! Please write 'Y' to start or 'N' to exit!\n")
else:
break
if start == "y":
separate(players)
else:
print("Goodbye!")
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 6 years ago.
I'm constructing an interactive timetable for terminal with Python, but at the end of my code where i have if, elif and else statements no matter what user input i give it keeps passing the if statement. Any Solutions would be greatly appreciated and Thank You for your time :)
while True:
TimeTable()
print "\nDo you wish to go again? "
answer = raw_input()
if answer == "Yes" or "yes":
print " "
continue
elif answer == "No" or "no":
print "Ok then"
break
else:
print "Ok then"
break
answer == "Yes" or "yes"
# is the same as
(answer == "Yes") or "yes"
and is always True. You can solve your problem this way:
answer in ["Yes", "yes"]
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 6 years ago.
So the problem I'm having is with this part of a simple script for a Chat Bot about Programming. But when I test this part of the script and type in 'B' as my input, it says ('Panda: I'm glad you share my enthusiasm.') which is what is mean't to happen if I input A! Can someone please point out to me what I'm doing wrong?
invalidAnswer2 = true
while invalidAnswer2 == true :
answer2 = input (user)
if answer2 == ('a') or ('A') :
print ('Panda: I'm glad you share my enthusiasm.')
invalidAnswer2 = false
elif answer1 == ('b') or ('B') :
print ('Panda: I wish. I am actually British, but I dream of going to America!')
print ('Panda: *Cough* Um anyway that was rather unproffesional of me.')
invalidAnswer2 = false
else :
invalidAnswer()
invalidAnswer2 = true
Thank You for helping me fix it. It was a simple typo after all that XD
The problem is your that your first if statement is always True:
if answer1 == "a" or "A"
Will always yield true, because it's really asking:
if (answer1 == "a") or ("A")
And "A" will always return True.
What you meant is probably:
if answer1 == "a" or answer1 == "A"
or the better option:
if answer1.lower() == "a"
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.
I am a new to Python, and I ran into some issues when developing a D&D Dice program.
import random
print("Hi,here you can roll all D´n´D Dice´s!")
dice=input("What dice do u want?D4,D6,D8,D10,D12,D20 or D100?")
if dice=="D4" or "d4":
print(random.randint(1,4))
elif dice=="D6" or "d6":
print(random.randint(1,6))
elif dice=="D8" or "d8":
print(random.randint(1,8))
elif dice=="D10"or"d10":
print(random.randint(1,10))
elif dice=="D12"or"d12":
print(random.randint(1,12))
elif dice=="D20"or"d20":
print(random.randint(1,20))
elif dice=="D100"or"d100":
print(random.randint(1,100))
else: print("No?Ok!")
When I run the program and enter the number of dice, it always enter the first if statement.
You want to write:
if dice=="D4" or dice=="d4":
instead of:
if dice=="D4" or "d4":
"d4" by itself has a (true) boolean value because it isn't an empty string.