Python Forum
C++ programmer confused about why Python isn't working the way I intend
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C++ programmer confused about why Python isn't working the way I intend
#1
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.
Reply
#2
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.
Radical and PyDan like this post
Reply
#3
Exclamation 
Thanks for the detailed breakdown
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  String int confused janeik 7 1,087 Aug-02-2023, 01:26 AM
Last Post: deanhystad
  I am confused with the key and value thing james1019 3 976 Feb-22-2023, 10:43 PM
Last Post: deanhystad
  Confused about python execution jpezz 4 1,390 Oct-09-2022, 06:56 PM
Last Post: Gribouillis
  Pandas confused DPaul 6 2,581 Sep-19-2021, 06:45 AM
Last Post: DPaul
  is and '==' i'm confused hshivaraj 6 2,728 Sep-15-2021, 09:45 AM
Last Post: snippsat
  Confused with 'flags' tester_V 10 4,941 Apr-12-2021, 03:03 AM
Last Post: tester_V
  Simple Tic Tac Toe but I'm confused Izith 1 2,205 Sep-26-2020, 04:42 PM
Last Post: Larz60+
  I am really confused with this error. Runar 3 3,039 Sep-14-2020, 09:27 AM
Last Post: buran
  Confused on how to go about writing this or doing this... pythonforumuser 3 2,502 Feb-10-2020, 09:15 AM
Last Post: snippsat
  New Programmer / Program ericvrocha 5 2,821 Oct-07-2019, 09:25 PM
Last Post: ericvrocha

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020