Click here to Skip to main content
15,921,382 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am doing beginner Python exercises, have never coded before.

I want to know why it doesn't work if I move part of the code inside the function (I already know it works if I write it before). specifically this part:
Python
num_1 = input("first number: ")
    num_2 = input("second number: ")
    int(num_1)
    int(num_2)


What I have tried:

Python
def multiplication_or_division(num_1, num_2):
    num_1 = input("first number: ")
    num_2 = input("second number: ")
    int(num_1)
    int(num_2)
    if num_1 * num_2 <= 1000:
        print(num_1 * num_2)
    else:
        print(num_1 + num_2)        
        
multiplication_or_division(int(num_1), int(num_2))
Posted
Updated 12-Sep-23 9:25am
v3
Comments
Patrice T 9-Sep-23 0:15am    
-What supposed to do ypur code ?
-What input fives you what output?
-What output did you expected ?
Use Improve question to update your question.

1 solution

You've defined multiplication_or_division to take two parameters, but when you call it:
Python
multiplication_or_division(int(num_1), int(num_2))
you try to pass it variables which don't exist - num_1 and num_2 are declared as the parameters to the multiplication_or_division function and only exists inside that function - you are supposed to pass them into the function so it can process them:
Python
def multiplication_or_division(num_1, num_2):
    if num_1 * num_2 <= 1000:
        print(num_1 * num_2)
    else:
        print(num_1 + num_2)
        
num_1 = input("first number: ")
num_2 = input("second number: ")
multiplication_or_division(int(num_1), int(num_2))
Or not pass them as parameters:
Python
def multiplication_or_division():
    num_1 = int(input("first number: "))
    num_2 = int(input("second number: "))
    if num_1 * num_2 <= 1000:
        print(num_1 * num_2)
    else:
        print(num_1 + num_2)
        
        
multiplication_or_division()
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900