-1

I have two scripts: rank.py and elo_7.py. The elo script pulls info from rank.

In elo_7.py:

...

elif player_1.age or player_2.age > 50 and abs(player_1.age-player_2.age) > 10:
    (make some adjustment)

In rank.py:

class Player:
    def __init__(self,name,age,rank_nogi,record,weight,school):
        self.name=name
        self.age=age
        self.rank_nogi=rank_nogi
        self.record=record
        self.weight=weight 
        self.school=school

player_1=Player('John',20,1600,0,91,'SJJ')
player_2=Player('Sally',29,1650,0,81,'SJJ')

Since the age difference is only 9 years and neither player is over 50 the elif statement should not execute and yet it does. Could someone please explain why?

On a side note, if I change the or to and then it will not execute.

DJK
  • 8,924
  • 4
  • 24
  • 40
  • Possible duplicate of [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – SiHa Feb 09 '19 at 15:44

2 Answers2

1

Its the way you are grouping the conditional statements. You need to change it to:

elif (player_1.age > 50) or (player_2.age > 50) and abs(player_1.age-player_2.age) > 
10:

It evaluates the conditionals short-circuited meaning that since player_1.age1 is on the left-side of the OR, and player_1.age is non-null (evaluates to true) that the elif is evaluated as true before doing the other checks. See section 3.8 of this https://www.pythonlearn.com/html-008/cfbook004.html

foobarbaz
  • 508
  • 1
  • 10
  • 27
0

Yes, it is the way you are grouping things. Its always evaluating to True and not doing what you expect. You can use the "any" operator to avoid writing the conditional multiple times.

elif any(x > 50 for x in [player_1.age, player_2.age]) and abs(player_1.age-player_2.age) > 10:
Tdaw
  • 181
  • 4