Python is telling me 66 <= 17

  • A+
Category:Languages

I'm new to python and trying to build a program that evaluates whether a potential partner is too young for someone using the / 2 + 7 rule.

despite using test variables that are well above 18 the program executes line 7 no matter what I do. I used 88/77, 77/66, 19/19, it always executes line 7.

num1 = float(input("What is the higher age number? ")) num2 = float(input("What is the lower age number? ")) output = num1 / 2 + 7 if num1 and num2 <= 17:     print("You're both underage") elif num2 <= 17:     print("You're going to jail bud") elif output <= num2:     print("That's OK") else:     print("They are slightly too young for you") 

EDIT:

I made the fix that many suggested, but now the program is still not working as intended and I found another flaw.

num1 = float(input("What is the higher age number? ")) num2 = float(input("What is the lower age number? ")) output = num1 / 2 + 7 if num1 <= 17 and num2 <= 17:     print("You're both underage") elif num2 <= 17:     print("You're going to jail bud") elif output <= num2:     print("That's OK") else:     print("They are slightly too young for you") 

when num1 = 19 and num2 = 16, the program outputs line 5 when I want it to output line 7. It's also still outputting line 7 when num1 and num2 are both set to a value higher than 17.

 


The expression :

if num1 and num2 <= 17: 

is like :

if num1 == True and num2 <= 17: 

With num1=66, num1 is similar to True and num2 <= 17 is not evaluated

To fix your program, You need write :

if num1 <= 17 and num2 <= 17: 

From python documention : Truth Value Testing

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: