You never defined x and y globally. You only defined it in the function when you did def smaller_num(x, y) .
When you execute smaller_num(x= input("Enter first number:-") ,y= input("Enter second number:-")) , you do not create variables named x and y , you just create parameters for your function.
To fix your code, create an x and y variable before you call your function:
def smaller_num(x, y): ## Can be rephrased to def smaller_num(x, y): if x > y: ## if x > y: number = y ## return y else: ## else: number = x ## return x return number x = input("Enter first number:-") y = input("Enter second number:-") result = smaller_num(x, y) print("The smaller number between " + str(x) + " and " + str(y) + " is " + str(result))
Another reason your code doesn't work is because you are not assigning the return value of the function back to the variable. When you return something from a function, and again, when you call the function, you need to assign a value to a variable, for example, I: result = smaller_num(x, y) .
When you called your function, you never assigned a value to a variable, so it was wasted.
Also, are you using Python 3 or 2.7? In python 3, input() will return a string, and to convert it to an integer, you can call int() around the input() function.
source share