2016-11-16 243 views
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)") 

我得到的錯誤是有沒有辦法讓這項工作?

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

完全忘掉'global'關鍵字,它會創建錯誤的代碼,請參閱[「爲什麼全局變量是邪惡的?」](http://stackoverflow.com/questions/19158339/why-are-global-variables-邪惡),嘗試以工作流的形式繪製你的程序,數據從哪裏來,你用它做了什麼,它在哪裏發生。 s的功能可以接收輸入並返回輸出,而不會觸碰其他任何東西。 – spectras

+0

如果你想調用一個函數''globalinputnum()'',你需要圓括號,因爲你沒有在這裏調用函數,'num1'永遠不會被定義,即使它是這樣,函數也沒有全局定義它。正因爲如此,你的代碼並不知道'num1'是什麼。 –

+0

'num1'在'if'塊內定義。通過範圍法律。 http://stackoverflow.com/questions/291978/short-description-of-scoping-rules –

回答

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

您還沒有真正叫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)) 
相關問題