Python Forum
C++ programmer confused about why Python isn't working the way I intend - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: C++ programmer confused about why Python isn't working the way I intend (/thread-40728.html)



C++ programmer confused about why Python isn't working the way I intend - Radical - Sep-14-2023

So I just started learning Python a few days ago and have written a very simple block of code, but it doesn't do what I think it would. Can you please walk me through what is actually happening here while it's checking the conditions?
while True:
    player = input()
    if player != ('a' or 'b' or 'c' or 'd'):
        print('whatever')
    else:
        break
So, what I would expect to happen is that if the player inputs anything other than a, b, c, or d, then it will print the text. And if they input one of a, b, c, d, then it will break the loop. But what ends up happening is if player inputs "a" it will break the loop, and neither b, c, or d will break the loop, but will instead send the text.

I would expect this to work the way it does in C++, where if I were to type:

while (true)
{
    string player = input();
    if (player != (a || b || c || d))
    {
        printf("whatever");
    }
    else
    {
        break;
    }
}
it would have the exact result I'm looking for.

So what is happening in the python code?? I don't want to have to type out "if player != 'a', and player != 'b', and player != 'c', etc.

(I am not really looking to fix my code, but rather for an explanation as to why my code is incorrect, and how it's actually being read/interpreted in Python) Thank you.


RE: C++ programmer confused about why Python isn't working the way I intend - deanhystad - Sep-14-2023

A or B does not return True or False. A or B returns A if bool(A) is True else it returns B. It doesn't even look at the value of B.

bool(object) is True for most objects. bool(object) is False for False, 0, None, empty list, empty tuple, empty dictionary, empty set and blank string.
(0 or 5) == 5
(1 or 5) == 1
([] or None) == None
(None or []) == []
(0 or None or False or []) == []
Evaluation of the last example.
(0 or (None or (False or []))) == (None or (False or [])) because bool(0) is False.
(None or (False or [])) == (False or []) because bool(None) is False.
(False or []) == [] because bool(False) is False.

Your code was testing if player == "a".
("a" or "b" or "c" or "d") == "a" because bool("a") is True.

"and" works similarly. A and B returns A if bool(A) is False, else it returns B. It doesn't even look at the value of B.
(0 and 5) == 0
(1 and 5) == 5
([] and None) == []
(None and []) == None
(0 and None and False and []) == 0
You probably want to use in().
    if player not in ("a", "b", "c", "d"):
        print('whatever')
    else:
        break
This reads better than the equivalent:
    if not (player=="a" or player=="b" or player=="c" or player=="d"):
        print('whatever')
    else:
        break
Oh, before I forget, your C++ code won't work either. The compiler would complain that you cannot do string || string.


RE: C++ programmer confused about why Python isn't working the way I intend - Radical - Sep-15-2023

Thanks for the detailed breakdown