有没有办法让这项工作成功?

时间:2016-11-16 07:11:43

标签: python python-3.x

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)")

我得到的错误是

---------------------------------------------------------------------------
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

我正在尝试创建一个基本计算器,使用定义的“globalinputnum”来根据它在输入中接收的操作来请求输入。但似乎我无法使其发挥作用:(

1 个答案:

答案 0 :(得分:-1)

在你的代码中:

#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))

如果您需要在名称后面加上parens,那么您实际上并未调用globalinputnum。

#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))