Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Flow Control Help
#4
You don't understand how "or" works. This is pretty common with new Python programmers. I will demonstrate how "or" works with some code.
def study_or(Player):
    print(f'{Player} == "R" is  {Player == "R"}')
    print(f'{Player} == "R" or "P" is  {Player == "R" or "P"}')
    print(f'{Player} == "R" or "P" or "S" is  {Player == "R" or "P" or "S"}')
    print(f'{Player} == "R" or "P" or "S" or "Q" is  {Player == "R" or "P" or "S" or "Q"}')
I run this code with Player = "R" and it prints this:
Output:
R == "R" is True R == "R" or "P" is True R == "R" or "P" or "S" is True R == "R" or "P" or "S" or "Q" is True
This is probably exactly what you expect to happen, so it is of little interest (for now)

Lets try Player = "P"
Output:
P == "R" is False P == "R" or "P" is P P == "R" or "P" or "S" is P P == "R" or "P" or "S" or "Q" is P
As expected, Player == "R" is False, but you didn't expect Player == "R" or "P" to be "P" did you?

From the documents : https://docs.python.org/3/library/stdtyp...and-or-not
Quote:x or y: f x is false, then y, else x
In your example, x is Player == "R" and y is "P". Player == "R" is False, so the "or" looks at "P". Is "P" True, or is it False? In Python most things are treated as "True" when used in a logic operation. The number of things treated as False is a much shorter list.

Things that are logic False:
Empty lists []
Empty tuples (,)
Empty dictionaries {}
Empty strings ""
Empty ranges range(0)
The number zero (0, 0.0, 0j)
None
False

"P" is a non-empty string, so it is True as far as our "or" statement. So if Player = "R", the "or" will return True (x is True), othewise the "or" will return "P". You may as well delete the or "S" or "Q" parts because they will never be evaluated.
ndc85430 likes this post
Reply


Messages In This Thread
Flow Control Help - by RoadToSensei - Feb-13-2022, 08:11 PM
RE: Flow Control Help - by menator01 - Feb-13-2022, 08:15 PM
RE: Flow Control Help - by RoadToSensei - Feb-13-2022, 09:02 PM
RE: Flow Control Help - by deanhystad - Feb-13-2022, 10:38 PM
RE: Flow Control Help - by BashBedlam - Feb-14-2022, 01:51 AM
RE: Flow Control Help - by deanhystad - Feb-14-2022, 08:21 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Alicat Flow Meter data acquisition marenr 0 422 Sep-29-2023, 10:59 PM
Last Post: marenr
  Node Flow Generation in Python Linenloid 0 740 Feb-21-2023, 07:09 PM
Last Post: Linenloid
  Understanding Raspberry PI RTS/CTS flow control zazas321 2 3,802 Oct-30-2020, 09:55 AM
Last Post: zazas321
  Understanding Raspberry PI RTS/CTS flow control zazas321 0 1,595 Oct-30-2020, 08:02 AM
Last Post: zazas321
  Reproducing assignment with min cost flow in networkx mntfr 0 2,211 Jun-13-2019, 04:06 PM
Last Post: mntfr
  Find Maximum Flow for graph with infinite capacities Dav 6 4,485 Apr-16-2019, 02:08 PM
Last Post: Dav
  program flow advice sampazzer 2 3,235 Aug-05-2017, 09:34 PM
Last Post: sampazzer
  Trouble with general code flow JCN 1 3,488 May-26-2017, 01:27 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

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