Can someone help with this simple code?

def did_that_help():
    print("Did the solution solve your problem?")
    response6 = input("Yes/No")
    if response6 == "Yes" or "yes:
        print("Thank you!")
    elif response6 == "No" or "no":
        it_did_not_help()
    else:
        print("Sorry, we couldn't understand you.")
        did_that_help()
        
def it_did_not_help():    
    print("Do your want to talk to a live representative or schedule a home visit? \n [1] live representative \n [2] schedule a home visit")
    response7 = input("Please enter the number corresponding to your choice")
    if response7 == "1":
        live_rep("support")
    elif response7 == "2":
        home_visit("support")
    else:
        print("Sorry, we couldn't understand you.")
        it_did_not_help()

When I call the function did_that_help() and gives the answer “no”, it will give me the answer “thank you!” instead of calling the function it_did_not_help().
Maybe I am using the operator “or” wrong? I just started out learning Python, so I just don’t have any clue. Can anybody help with this?

Hi and welcome!

Does fixing this typo help?
if response6 == "Yes" or "yes: -> if response6 == "Yes" or "yes":
(missing " after "yes)

1 Like

That is not how or works. or is a binary comparison operator. It works from left to right and If both operands (the objects on the left and right of or) are reducible to True or False, it will work the way you think. However, in this case, the two operands are strings.

response = "no"
if response == "Yes" or "yes":
    print("response was yes")
else:
    print("response was no")

# Output:
response was yes

Python assigns the “truth value” False to 0 (zero), False, any empty container (list, string or dictionary, for instance), or any expression that evaluates to one of these.

Every other object (e.g., the strings "Yes’ and “yes”) has a “truth value” of True.

The or operator returns the value of the first operand that returns True. If neither is True, it returns the value of the first that returns False. (Edit: wrong. Should be first True or second False; h/t mtf, below.)

The and operator returns the value of the first operand that returns False; if neither, it returns the value of the second True.

response = "Yes" or "yes"
print(response)
response = 0 or "yes"
print(response)
response = "Yes" and "yes"
print(response)
response = 0 and "yes"
print(response)

# Output:
Yes
yes
yes
0

Bottom line: what you need is :

if response == "Yes" or response == "yes":
1 Like

Actually, the last value in the expression is returned.

>>> [] and {}
[]
>>> [] or {}
{}
>>> 
1 Like

Ah! Correct, thanks. or returns first True or second False. For both and and or, the value returned, due to “lazy evaluation” is the first value (left-to-right) that determines the and or or condition.

1 Like
>>> [] or '' or 0 or None or () or {}
{}
>>> 

Now consider this…

>>> [1] and '1' and 1 and (None) and {}
>>> 

A list, tuple or set containing only None is treated as empty.

Notice how that is where the expression short-circuited?

falsy short-circuits AND; truthy short-circuits OR.

It matters not how many operands.

If no short-circuits occur then the last value is the result.

>>> [1] and '1' and 1 and (not None) and {}
{}
>>> 

And,

>>> [] or '' or 0 or None or (0) or {}
{}
>>> [] or '' or 0 or None or (1) or {}
1
>>> 
1 Like

Well put!

I tend to think of and and or as binary, but chaining certainly works without introducing ambiguity, due to associativity.

1 Like

Precisely, and why the order is arbitrary, though we may need to stipulate the order to get the outcome that is most favorable to our objective.

Just to clarify,

if a == 'astring' or 'Astring'

will always be True.

if a == 'astring' or a == 'Astring'

Note that both operands are comparison expressions.