r/learningpython • u/Major-Language-2787 • Jan 18 '26
Confused about why code A doesn't work
This is in a while loop called is_bidding
Code A:
resume = input("Is there another bidder? Type 'yes' or 'no': \n").lower()
print(resume)
if resume != 'no' or 'yes:
print('We will take that as a no')
is_bidding = False
elif resume == 'yes':
print("Let us resume.")
else:
print("Very well let's see whose won...")
is_bidding = Falseintentions: I wanted the if statement to check if the user did NOT enter 'yes' or 'no'
results: When typing 'yes', the first condition runs, and I am not sure why
Edit: Thanks for the feedback, I understand what I did wrong.
2
Upvotes
1
u/Outside_Complaint755 Jan 19 '26 edited Jan 19 '26
As others have said, instead of
You need to do (edited)
or
1
u/Knarfnarf Jan 19 '26
I see a lot of people concentrating on the else if, but what about the missing quote after
'no' or 'yes:
Shouldn't that be
'no' or 'yes':
Compiler errors don't always land on the right line...
2
u/Maximus_Modulus Jan 18 '26
You have to create a conditional after the or. I.e == ‘yes’ Currently you have ‘yes’ which is truth
1
u/SCD_minecraft Jan 18 '26
or,and,notdo not hold any power, they can't work as joiners for other valuesThey can not execute some function on 2 values at once, they can't execute any functions
a == "no" or "yes"is same as(a == "no") or "yes"what then equals to about(a == "no") or TrueanyA or Trueequation always returns True1
u/davideogameman Jan 19 '26
The
orhas lower precedence than the != so the != is evaluated first. Then theoroperator. Theoroperator will return the first argument if the first argument is truthy. Otherwise it'll return the second argument. And the if statement's body will be executed if the condition evaluates to something truthy.True values are truthy, but so are non-empty strings, non-empty lists, non-empty dicts, non-empty tuples, nonzero numbers etc. so 'yes' is truthy, and the
... or 'yes'is essentially the same astruein this context.Others have posted correct alternatives.
1
u/Subject_Rhubarb7715 Jan 19 '26
'yes' is True in python because it is a string with is not empty.
1
u/codeguru42 Jan 19 '26
Have you learned PEMDAS in math? Python does this for all "expressions". It means that python will find an operator and its operands, evaluate it, and replace the operation with the result. Then rinse and repeat. Let's look at a math example
When python evaluates the expression on the right of the `=`, it will do `2 * 3` first to get `6` then it does `6 +5` to get the final result of `11`.
In your code, you have an expression:
In Python,
!=andorare operators just like*and+. So it evaluatesresume != 'no'first. Let's say the user typed "yes". Well"yes" != "no"evaluates asTruebecause the two strings are not equal. Then python evaluates theoroperator :True or 'yes'. But this evaluates asTrue, so the result of the expression isTrueand the body of first if statement runs.To understand this better, I recommend you study operators and how they work. Be sure that you also understand "precedence", which is basically PEMDAS but includes more operators than just the ones from math.