Some confusion in below code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Some confusion in below code

def apple(): print('in apple') def mango(): print('in mango') b=input('enter the num: ') print(b) if b==0: apple() else: mango() output enter the number: 0 0 in mango y in mango it shld be in apple right y input function taking as an string

20th Feb 2018, 3:38 PM
Pradeep Vc
Pradeep Vc - avatar
4 Answers
+ 2
Because variable 'b' is storing a String, not an int. You'll just want to convert the input to an int data type instead of a string type. b = int(input('enter the num: ')) ^int() is a function that'll convert it to an int. Just wrap your input statement inside of the int() function. When you store it as a string and do no convert it, your IF check is seeing if it's equal to 0. Since a string isn't a number, it'll return false and execute the ELSE statement, which is to call the mango() function. Hope that helps. FULL CODE: def apple(): print('in apple') def mango(): print('in mango') b = int(input('enter the num: ')) print(b) if b == 0: apple() else: mango()
20th Feb 2018, 3:49 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 2
Tnx, so input function returns only a string??
20th Feb 2018, 3:51 PM
Pradeep Vc
Pradeep Vc - avatar
+ 2
You're welcome, Pradeep. Yes, by default it's going to return a string value unless you specify otherwise.
20th Feb 2018, 4:35 PM
Fata1 Err0r
Fata1 Err0r - avatar
0
ok got it
20th Feb 2018, 4:36 PM
Pradeep Vc
Pradeep Vc - avatar