Just starting to understand the Python, stumbled upon a small problem.

def minimum(a, b): a = int(input('Type number a: ')) b = int(input('Type number b: ')) if a > b: print (b) elif a == b: print ('a equals to b') else: print (a) minimum(a,b) 

When I run the code, it gives an error:

Traceback (most recent call last): File "test.py", line 12, in minimum (a, b) NameError: name 'a' is not defined

Python version 3.5.1 .
Thank you in advance.

    3 answers 3

    When calling a function

     minimum(a,b) 

    the python does not find the variable, but since it is not in the code. That which is in function is not considered.

      a = int(input('Type number a: ')) b = int(input('Type number b: ')) 

    need to take out of function

    • Thank you, learned. - dxdy

    By code it is clear that the variables a and b will be received from user input ( input function). Therefore, it makes no sense to pass these variables as function arguments. Here is an example:

     def minimum(): a = int(input('Type number a: ')) b = int(input('Type number b: ')) if a > b: print (b) elif a == b: print ('a equals to b') else: print (a) minimum() 

    If you do not need to receive these variables from user input, then you do not need to use the input function. And variables a and b need to be transferred manually. Here is an example:

     def minimum(a, b): if a > b: print (b) elif a == b: print ('a equals to b') else: print (a) minimum(5, 1) 

    Specifically, the error that gives you indicates that the variable you are trying to use does not exist yet.

     minimum(a, b) 

    When calling a function, there is no variable neither a nor b . They will be defined only locally, inside this function, after its call.

      Before using variables, they need to be given a value and for Python it is just a symbol and not a variable enter here the code

       a=0 b=0 def minimum(a, b): a = int(input('Type number a: ')) b = int(input('Type number b: ')) if a > b: print (b) elif a == b: print ('a equals to b') else: print (a) minimum(a,b)