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