+ 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
4 Respuestas
+ 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()
+ 2
Tnx, so input function returns only a string??
+ 2
You're welcome, Pradeep.
Yes, by default it's going to return a string value unless you specify otherwise.
0
ok got it