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
#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.
PyDan and Radical like this post
Reply


Messages In This Thread
RE: C++ programmer confused about why Python isn't working the way I intend - by deanhystad - Sep-14-2023, 06:11 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  String int confused janeik 7 2,540 Aug-02-2023, 01:26 AM
Last Post: deanhystad
  I am confused with the key and value thing james1019 3 1,857 Feb-22-2023, 10:43 PM
Last Post: deanhystad
  Confused about python execution jpezz 4 2,639 Oct-09-2022, 06:56 PM
Last Post: Gribouillis
  Pandas confused DPaul 6 3,772 Sep-19-2021, 06:45 AM
Last Post: DPaul
  is and '==' i'm confused hshivaraj 6 3,690 Sep-15-2021, 09:45 AM
Last Post: snippsat
  Confused with 'flags' tester_V 10 6,763 Apr-12-2021, 03:03 AM
Last Post: tester_V
  Simple Tic Tac Toe but I'm confused Izith 1 3,398 Sep-26-2020, 04:42 PM
Last Post: Larz60+
  I am really confused with this error. Runar 3 4,038 Sep-14-2020, 09:27 AM
Last Post: buran
  Confused on how to go about writing this or doing this... pythonforumuser 3 3,207 Feb-10-2020, 09:15 AM
Last Post: snippsat
  New Programmer / Program ericvrocha 5 3,884 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