This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 1 year ago.
code = (input("Write a number: "))
print (code)
if code == "1" or "2":
print ("bad")
elif code =="3":
print ("good")
else:
print ("hmmm" )
Output:
Write a number: 3
3
bad
I'm confused, shouldn't the output be "good" instead of "bad"?
The syntax of your logic is wrong:
if code=="1" or code=="2":
What you originally had (if code == "1" or "2") is understood by the interpreter as asking the following two questions:
Does the value in code == "1"?
Is "2" true?
The second question will always evaluate to True. So the if clause will always run.
Related
This question already has answers here:
Python SyntaxError: invalid syntax elif statement [closed]
(3 answers)
python : if statement and multiple variable values [duplicate]
(2 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 5 months ago.
I'm trying to have the user enter their name and client stock profit information for as many clients as the user wants.The loop will go around until the user answers no for if they have another client they want to enter. Then it will print the average of all the clients stock information. I'm just not sure how to get the loop to stop properly and execute the rest of the code.
csum=0
ccount=0
clients=int(input("How Many Clients?"))
while clients==clients:
sum=0
counter=0
determiner=str(input("Do you have another client to enter?"))
clientname=str(input("What is the client's name?"))
if determiner==str('Yes' or "Y" or "YES"):
for i in range(4):
stock=int(input("Enter Stock Amount:"))
sum=sum+stock
counter=counter+1
avg=sum/counter
csum=csum+avg
ccount=ccount+1
print("The Average for",clientname,"is:",avg)
elif determiner==str('No' or 'no' or 'NO'):
cavg=csum/ccount
print("The average for all the clients is:",cavg)
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 10 months ago.
n=[0]*365
s=0
s1=1
s2=2
s3=3
s4=4
s5=5
s6=6
for i in range(len(n)):
n[i]=4+1
s=s+7
s1=s1+7
s2=s2+7
s3=s3+7
s4=s4+7
s5=s5+7
s6=s6+7
if n[i]==s:
n[i]=0
elif n[i]==s1:
n[i]=1
elif n[i]==s2:
n[i]=2
elif n[i]==s3:
n[i]=3
elif n[i]==s4:
n[i]=4
elif n[i]==s5:
n[i]=5
elif n[i]==s6:
n[i]=6
Hey everyone here is my code i need to imput which number in row in array it is and it needs print out the number like if i input 300 it needs to output the 300th number in the list
I have tried using print(n[input()] but that obvioslly didnt work can you please help me
It is because input returns a string. The following should work:
print(n[int(input())])
However, note that this will fail if you input something else than a number as python will fail in the int conversion.
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
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 last year.
Here is my code so far - I am simply trying to say that if the input isnt one of these words, to redo the loop. However, although 'monday' works, the other days dont.
run_day = input("On which day do you want your code to run? ")
while run_day.lower() != ("monday" or "tuesday" or "wednesday" or "thursday" or "friday" or "saturday" or "sunday"):
run_day = input("On which day do you want your code to run? ")
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.
I'm just writing a simple if statement. The second line only evaluates to true if the user types "Good!"
If "Great!" is typed it'll execute the else statement. Can I not use or like this? Do I need logical or?
weather = input("How's the weather? ")
if weather == "Good!" or "Great!":
print("Glad to hear!")
else:
print("That's too bad!")
You can't use it like that. The or operator must have two boolean operands. You have a boolean and a string. You can write
weather == "Good!" or weather == "Great!":
or
weather in ("Good!", "Great!"):
What you have written is parsed as
(weather == "Good") or ("Great")
In the case of python, non-empty strings always evaluate to True, so this condition will always be true.
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.
Oh, man! Sorry, I'm quite new to Python. I was just being an idiot. So don't answer this.
in Python I tried to use the if/else function, but it always chose the "if" option. I use Python 2 (IDLE). How can you help? Here is my code:
if answer=="Yes" or "yes":
upload_name=raw_input("What do you want to change it to? ")
elif answer=="No" or "no":
print "Okay. Your name will remain as " + str(real_name) + "."
if answer=="Yes" or answer== "yes":
elif answer=="No" or answer== "no":
Or just use if answer.lower() == "yes"
or "yes" always evaluates to True once the string is a non empty string, you are not comparing to answer, you are testing bool("yes").
In [1]: bool("yes")
Out[1]: True
In your case I would just call lower on the string but if you have multiple values to test against using in can be a nice way to do it:
if answer in {"Yes","yes"}: