logic not working | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

logic not working

Can you tell why this program is not executing the else part even after the condition fails? is_age = bool(input("Are you 18 or above? :")) is_licensed = bool(input('Do you have a valid License? :')) print(type(is_age),type(is_licensed)) if is_age and is_licensed: print('You are eligible to drive') else: print('You are not eligible to drive') print('END') in the above code, even if I give both inputs as 'False', it executes the True condition. , However, if I manually assign True and false value to the variables it works as expected, Why is that?

1st Jul 2020, 8:15 AM
Ria sharma
Ria sharma - avatar
3 Answers
+ 3
A non-empty string is always True. If you just hit "enter" twice...without actually entering any values, the output will be 'You are not eligible to drive'.
1st Jul 2020, 8:23 AM
rodwynnejones
rodwynnejones - avatar
+ 2
Yes, at the conditional where you check both of your variables reads like: "if is_age and is_licensed, then print 'You are eligible to drive'. " Think about whatever booleans are associated with each variable. if you put both as False, it'll look like: if False and False, print 'You are eligible to drive'. wich it is, and its also why True and False doesnt work. Nothing is True AND False, so it printed that you cant drive.
1st Jul 2020, 8:23 AM
Slick
Slick - avatar
+ 2
Thank you so much every one for your answers
1st Jul 2020, 9:59 AM
Ria sharma
Ria sharma - avatar