What is the issue? Why output is not 1. | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

What is the issue? Why output is not 1.

Code:- a = bool(input("f1: ")) b = bool(input("f2: ")) print(a+b) Output:- f1: True f2: False 2

7th Aug 2022, 4:31 PM
Anshul Khangar
Anshul Khangar - avatar
2 Respostas
+ 6
the reason of this behaviour is NOT because input() functions returns always True. input function returns always a string. the input we give in the sample code is `True` and `False`. this is a string, and not a real boolean value. any non-empty string is considered as a `truthy value`, so when we use `bool()` to convert our input it returns True, which is a value of 1. we can input whatever string we like, the result will always be 1 for each of the input. the result of 1 + 1 = 2 so if we input: `eggs and spam` `tomato` the result will be same, as both are non-empty strings
7th Aug 2022, 9:21 PM
Lothar
Lothar - avatar
+ 2
Anshul Khangar Because input() function always returns String so bool(String) will be always True So bool(input()) = 1 So a = 1 + 1 = 2
7th Aug 2022, 4:35 PM
AĶ¢J
AĶ¢J - avatar