Python Forum
return statement will not work
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
return statement will not work
#1
I have a problem with python 3.x.

I am trying to make a CLI calculator using python 3.x, and tried to use the return statement.

However, even after I use return , it gives me an error that my variables have not been defined.

Does anyone know how to fix this? Here is a snippet of my code:

def getNum(): #ask for two numbers and then return to function
    n1 = int(input("Please enter the first number: "))
    n2 = int(input("Please enter the second number: "))
    return n1, n2
def multi(): #multiplication
    getNum()
    print("\nThat equals...")
    print(n1 * n2)
multi()
Reply
#2
It is working, but the returned values aren't stored anywhere. Add a couple variables:

def getNum(): #ask for two numbers and then return to function
    n1 = int(input("Please enter the first number: "))
    n2 = int(input("Please enter the second number: "))
    return n1, n2

def multi(): #multiplication
    n1, n2 = getNum()
    print("\nThat equals...")
    print(n1 * n2)

multi()
Reply
#3
Thank you so much @stullis!! You are so much help.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help with Return statement Columbo 13 2,288 Sep-17-2022, 04:03 PM
Last Post: Columbo
  getting an import statement to work in my program barryjo 1 1,663 Dec-06-2021, 04:28 PM
Last Post: snippsat
  How to invoke a function with return statement in list comprehension? maiya 4 2,847 Jul-17-2021, 04:30 PM
Last Post: maiya
  Why doesn't this print statement work? stylingpat 10 5,756 Mar-23-2021, 07:54 PM
Last Post: buran
  syntax error on return statement l_butler 5 3,110 May-31-2020, 02:26 PM
Last Post: pyzyx3qwerty
  Newbie to Python Why does this if statement not work? Wrightys99 5 2,592 Mar-26-2020, 12:04 PM
Last Post: buran
  HELP! Return Statement Debugging HappyMan 5 3,126 Jan-27-2020, 07:31 PM
Last Post: michael1789
  why does this if-statement not work? sandeen 2 2,057 Nov-15-2019, 03:17 PM
Last Post: sandeen
  Embedding return in a print statement Tapster 3 2,284 Oct-07-2019, 03:10 PM
Last Post: Tapster
  return statement usage SB_J 3 2,430 Jul-16-2019, 07:24 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020