0

If Else im confused

age = 16 is_male = True weight = 75 if age > 18: if is_male: print("ok") else: print("not ok") elif weight > 72 and weight < 79: print("ok") else: print("not ok") #why does it output true if age is under 18

28th Sep 2024, 12:42 AM
Mohammad Badra
2 ответов
+ 1
Mohammad Badra when indented correctly I get OK on the weight ... Your code also looks beyond age and the if else to the elif of the code as weight ( 75 ) > 72 and less than 79: I currently do not see how you are obtaining "true" when none of your print statements relates to boolean response 🤔 only OK which is never used.
28th Sep 2024, 2:57 AM
BroFar
BroFar - avatar
+ 1
The problem is that you have 3 variables, but you wrote the if-else conditions in a disjointed manner. Perhaps you wanted to print ok only if age>18, is_male==True and weight is between 72 and 79? Then maybe you can write age = 16 is_male = True weight = 75 if all((age>18, is_male,weight in range(73,79))): print('ok') else: print('not ok')
28th Sep 2024, 7:52 AM
Bob_Li
Bob_Li - avatar