0
def globalinputnum():
global all_list
global choice
if choice in itertools.chain(subtraction, addition):
    num1 = int(input("ENTER FIRST NUMBER"))
    num2 = int(input("ENTER SECOND NUMBER"))
elif choice in itertools.chain(multiplication, division):
    if choice in multiplication:
        num1 = int(input("ENTER MULTIPLIER NUMBER"))
        num2 = int(input("ENTER MULTIPLICAND NUMBER"))
    else:
        num1 = int(input("ENTER DIVIDEND NUMBER"))
        num2 = int(input("ENTER DIVIDOR NUMBER"))
elif choice in itertools.chain(square,root):
    num3 = int(input("ENTER A NUMBER"))



"""Dictionaries"""
#Warning:Extremely messy!
division = ["Division","Divide","/","div"]
multiplication = ["*","x","times","multiply","multiplication","multiple"]
subtraction = ["-",'minus','subtract','subtraction']
addition = ['+','plus','addition','add']
root = ['root','squareroot','square root']
square = ['square','squared','power 2']
basic_double = [division,multiplication,subtraction,addition]
basic_single = [root,square]
basic_specific = [division, multiplication]
all_list = [division,multiplication,subtraction,addition,root,square]


#Code
while repeat_option in repeat:
choice = input("Input type of operation!(+,-,*,/,^2,^1/2)")
globalinputnum
if choice in addition:
    print(num1,"+",num2,"=",(num1+num2))
elif choice in subtraction:
    print(num1,"-",num2,"=",(num1-num2))
elif choice in division:
    num1 = int(input("ENTER DIVIDEND NUMBER"))
    num2 = int(input("ENTER DIVIDOR NUMBER"))
    print(num1,"/",num2,"=",(num1/num2))
elif choice in multiplication:
    num1 = int(input("ENTER MULTIPLIER NUMBER"))
    num2 = int(input("ENTER MULTIPLICAND NUMBER"))
    print(num1,"*",num2,"=",(num1*num2))
elif choice in square:
    print(num3,"^2","=",(num3**2))
elif choice in root:
    print(num3,"^(1/2)","=",(num3**(1/2)))
else:
    print("something went wrong!", math.pi)
repeat_option = input("Do you want to perform another calcualtion?(Y/N)")

the error I am getting is

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-11-4abd45f9246e> in <module>()
 61     globalinputnum
 62     if choice in addition:
---> 63         print(num1,"+",num2,"=",(num1+num2))
 64     elif choice in subtraction:
 65         print(num1,"-",num2,"=",(num1-num2))

NameError: name 'num1' is not defined

I'm trying to create something that a basic calculator here, using the defined "globalinputnum" to ask for an input depending on the operation it receives in the input. However it seems that I am unable to make it work :(

  • 1
    Forget about the `global` keyword entirely. It creates bad code. See [“why are global variables evil?”](http://stackoverflow.com/questions/19158339/why-are-global-variables-evil). Try to draw your program in the form of a workflow. Where data gets from, what you do with it, where it goes. Break up operations in functions that take inputs and return outputs and touch nothing else. – spectras Nov 16 '16 at 07:17
  • You need parentheses if you want to call a function: `globalinputnum()` because you aren't calling the function here, `num1` is never defined, and even when it is, the function does not define it globally. Because of this, your code doesn't know what `num1` is. – Elliot Roberts Nov 16 '16 at 07:18
  • 'num1' is defined inside an 'if' block. Go through scope laws for that. http://stackoverflow.com/questions/291978/short-description-of-scoping-rules – Sachin Nov 16 '16 at 07:22

1 Answers1

-1

In you code:

#Code
while repeat_option in repeat:
choice = input("Input type of operation!(+,-,*,/,^2,^1/2)")
globalinputnum
if choice in addition:
    print(num1,"+",num2,"=",(num1+num2))

You haven't actually called globalinputnum, to call if you need to have parens after the name.

#Code
while repeat_option in repeat:
choice = input("Input type of operation!(+,-,*,/,^2,^1/2)")
globalinputnum()
if choice in addition:
    print(num1,"+",num2,"=",(num1+num2))
Silver
  • 1,327
  • 12
  • 24