2016-03-25 20 views
2

我試圖用python編寫一個程序,它使用幾種不同的操作來計算(可能非常低效,但我離題)。但是,運行時出現了一個我無法弄清的錯誤。我認爲它需要定義一個變量的類型。 程序:我在python計算器中犯的錯誤

import math 
print('Select a number.') 
y = input() 
print('Select another number.') 
x = input() 
print('Select what operation you wish to perform. (e for exponentiation, d for division, m for multiplication, a for addition, s for subtraction, mo for modulo, l for log (the base is the first number you entered), r for root)') 
z = input() 
if z == 'e' or z == 'E': 
    print('The answer is ' + y**x) 
elif z == 'd' or z == 'D': 
    print('The answer is ' + y/z) 
elif z == 'm' or z == 'M': 
    print('The answer is ' + y*x) 
elif z == 'a' or z == 'A': 
    print('The answer is ' + y+x) 
elif z == 's' or z == 'S': 
    print('The answer is ' + y-x) 
elif z == 'mo' or z == 'Mo': 
    print('The answer is ' + y%x) 
elif z == 'l' or z == 'L': 
    print('The answer is ' + math.log(x,y)) 
elif z == 'r' or z == 'R': 
    print('The answer is ' + y**(1/x)) 

,在外殼出現了錯誤:

Traceback (most recent call last): 
    File "C:/Users/UserNameOmitted/Downloads/Desktop/Python/Calculator.py", line 7, in <module> 
    z = input() 
    File "<string>", line 1, in <module> 
NameError: name 'd' is not defined 
+0

使用'raw_input'而不是'input'。 – Lafexlos

+1

你確定嗎?該行號很奇怪 - 我得到[在線](https://repl.it/languages/python3)的錯誤是「Traceback(最近調用最後一次): 文件」python「,第11行,在 TypeError: /:'str'和'str'「不支持的操作數類型。這表明這是一個dup http://stackoverflow.com/questions/24412000/python-type-error-unsupported-operand – usr2564301

回答

1

您在這裏有幾個問題:

  1. z應該raw_input,不input輸入。
  2. 在劃分的情況下,您將劃分y/z而不是y/x
+0

OP正在使用python2.7。所以'input'給出一個'int' – itzMEonTV

+0

@itzmeontv arg,對。那真是愚蠢。 – Mureinik

1

你要做

z = raw_input() 

而且輸入收益爲intpython2.x。所以使用raw_input來讀爲str然後到處像print('The answer is ' + y**x)分成print('The answer is ' + str(y**x))

+0

怎麼樣爲math.log(x,y)?會變成str(math.log(x,y))嗎? – Mathime

+0

此外,這將產生16/5 = 3。 – Mathime

+0

如果你想要小數的答案,請從'__future__進口部門'在您的文件頂部 – itzMEonTV

1

你的代碼有錯誤

  1. 使用的raw_input()把字符串輸入,而不是輸入()
  2. 使用int(的raw_input())取整數輸入,它不是一個錯誤,但它是很好的做法。
  3. 你試圖用字符串除int。
  4. 您試圖連接字符串和整數。

你的代碼應該是這樣的。

import math 
print('Select a number.') 
y = int(raw_input()) 
print('Select another number.') 
x = int(raw_input()) 
print('Select what operation you wish to perform. (e for exponentiation, d for division, m for multiplication, a for addition, s for subtraction, mo for modulo, l for log (the base is the first number you entered), r for root)') 
z = raw_input() 
if z == 'e' or z == 'E': 
    print('The answer is %d' %(y**x)) 
elif z == 'd' or z == 'D': 
    print('The answer is %d' %(y/x)) 
elif z == 'm' or z == 'M': 
    print('The answer is %d' %(y*x)) 
elif z == 'a' or z == 'A': 
    print('The answer is %d' %(y+x)) 
elif z == 's' or z == 'S': 
    print('The answer is %d' %(y-x)) 
elif z == 'mo' or z == 'Mo': 
    print('The answer is %d' %(y%x)) 
elif z == 'l' or z == 'L': 
    print('The answer is %d' %(math.log(x,y))) 
elif z == 'r' or z == 'R': 
    print('The answer is %d' %(y**(1/x)))