+ 2

python

a = 19 if a < 12: print("Good morning..") elif a >= 12 <= 18: # the shortcut is not working if i do like that a >= 12 and a<= 18: its working fine print("Good evening") else: print("Good night")

14th Dec 2022, 10:51 AM
Amol Bhandekar
Amol Bhandekar - avatar
7 Answers
+ 4
suppose a = 14, then 14 <= 12 <= 18 is False 12 <= 14 <= 18 is True
14th Dec 2022, 11:23 AM
Lisa
Lisa - avatar
+ 5
if a = 19 then the condition would need to be 12 <= a <= 18
14th Dec 2022, 11:00 AM
Lisa
Lisa - avatar
+ 4
Amol Bhandekar , my code sample was not correct - sorry. since Lisa has already posted the correct version, i have deleted mine.
14th Dec 2022, 11:27 AM
Lothar
Lothar - avatar
+ 3
Amol Bhandekar If you want to check two conditions logical operator is needed...
14th Dec 2022, 10:55 AM
Riya
Riya - avatar
+ 3
Thanks all of you :) Please correct me if I'm wrong the both statements are same or correct just we have changed writing style elif a <= 12 <= 18: elif 12 <= a <= 18
14th Dec 2022, 11:17 AM
Amol Bhandekar
Amol Bhandekar - avatar
+ 1
Thanks @lisa understood :)
14th Dec 2022, 11:26 AM
Amol Bhandekar
Amol Bhandekar - avatar
0
elif 12<= a <= 18 :
16th Dec 2022, 1:09 AM
Ayach Madrid
Ayach Madrid - avatar