How do you use the input function with the def function?

I am new to programming. I have a little problem with python coding (with def function).

Thus, basically the code should give a smaller number of 2 numbers.

Question: Enter the codes to perform the following tasks:

  • Define a small_num function that takes two numbers to determine and return the smaller number of them.
    Ask user about two digits
    Use this function to determine a lower number and display the result.

So, I had user input instead of calling a function and adding a value inside a variable.

This is what my code looks like:

def smaller_num(x,y): if x>y: number= y else: number= x return number smaller_num(x= input("Enter first number:-") ,y= input("Enter second number:-")) print("The smaller number between " + str(x) + " and " + str(y) + " is " + str(smaller_num)) 

How to fix it? It does not work yet, because "x" is not defined. But I feel that I have clearly defined them in both the def function and the input function. So how can I fix this?

Thanks to those who answer this question.

+5
source share
4 answers

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.

+6
source

This will work:

 def smaller_num(x,y): if x>y: number= y else: number= x return number x = input("Enter first number:-") y = input("Enter second number:-") smaller = smaller_num(x,y) print("The smaller number between " + str(x) + " and " + str(y) + " is " + str(smaller)) 
+3
source

this should work:

 def smaller_num(x,y): if x>y: number = y else: number = x return number x = input("Enter first number:-") y = input("Enter second number:-") print("The smaller number between " + str(x) + " and " + str(y) + " is " + str(smaller_num(x,y))) 
+1
source

you cannot use the assignment operator inside def, but

 print(str(smaller_num(input("Enter first number:-"),input("Enter second number:-")))) 

works great.

0
source

All Articles