1

Altough Num1 is defined it is displaying an error saying it is not :( i have tried this with and without int() but still produces the same error. Just for context this is a calculator.

def main():
    Num1 = int(input("Please type First Number:"))
    calc = input("x,+,-,/")
    Num2 = int(input("Please type Second Number:"))

    if(calc == "x"):
        multiply()
def multiply():
    Num1 * Num2
  • you might want to read python's [LEGB scope rules](http://spartanideas.msu.edu/2014/05/12/a-beginners-guide-to-pythons-namespaces-scope-resolution-and-the-legb-rule/) – Pynchia Dec 21 '15 at 22:23

3 Answers3

2

Num1 and Num2 are defined in the scope of main, not in multiply. You would need to pass those in.

I should also point out that your multiply function doesn't return anything either

sedavidw
  • 11,116
  • 13
  • 61
  • 95
1

It is defined locally in another function, you need to have a global variable and use

def multiply():
    global num1

But the true solution is, you should make multiply function take arguments, like so

def multiply(a, b):
    return a * b

and you should call it

print multiply(num1, num2)

Edit: Call it inside the main() function of course, without global variables.

Rockybilly
  • 2,938
  • 1
  • 13
  • 38
0

Num1 is defined, but in another scope. In your case it's a function main(). You should either pass this two variables as a parameters or make them global.

Related: Short Description of Python Scoping Rules

Community
  • 1
  • 1
m0nhawk
  • 22,980
  • 9
  • 45
  • 73