0

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=input("hey ill take u to next fuction")
    if(x==1):
      test2()
test1()
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

2 Answers2

0
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"

ODiogoSilva
  • 2,394
  • 1
  • 19
  • 20
0

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 :

def test1():
    x=input("hey ill take u to next fuction")
    try :
       if(int(x)==1):
          test2()
    except ValueError:
       print 'enter a valid number'
Mazdak
  • 105,000
  • 18
  • 159
  • 188