If Condition Not Working Properly Inside A Function Python 3
This is the following code.The test2 function doesnt get invoked why? Even if i enter 1 the test2 fuction is not called def test2(): print('here i come') def test1(): x=inp
Solution 1:
x=input("hey ill take u to next fuction")
x
will be a string type, not an integer. You should change the if
statement to compare the same types (either converting x
to int
, or 1
to "1"
Solution 2:
Because you are comparing a string (your input) with 1 that is a integer.So you need to convert the input to int
and then compare it.
Also as it can raise ValueError
you can use a try-except
to handle that :
deftest1():
x=input("hey ill take u to next fuction")
try :
if(int(x)==1):
test2()
except ValueError:
print'enter a valid number'
Post a Comment for "If Condition Not Working Properly Inside A Function Python 3"