print("Welcome to MusCalculator")
print("For adding two no type add")
print("for mutiplying type multiply")
print("for subtraction type subtract")
print("for dividing type divide")
def add_numbers(x, y):
if input == "addition":
addition = x + y
print addition
if input == "multiply":
multiply = x*y
print multiply
if input == "subtract":
if x > y:
sub = x - y
print sub
else:
sub = y - x
print sub
if input == "divide":
div = x/y
print div
else:
print("Use me if you know me")
x = input("First No.")
y = input("Second No.")
addition = x + y
c = input("Type Operation")
add_numbers(x, y)
誤差構建兩位數的簡單操作型計算器?停留在錯誤
Traceback (most recent call last):
File "C:\Users\Aafaq\workspace\python\project 1.py", line 34, in <module>
c = input("Type Operation")
File "C:\Program Files (x86)\eclipse\plugins\org.python.pydev_4.5.5.201603221110\pysrc\pydev_sitecustomize\sitecustomize.py", line 141, in input
return eval(raw_input(prompt))
File "<string>", line 1, in <module>
NameError: name 'addition' is not defined
'input'是一個功能 - 所以你不能把它作爲一個變量(你還沒有定義)。將'c'作爲參數傳遞給'add_numbers'並用它來查看用戶的選擇是什麼。另外,你的'if'和'else'結構會搞砸。你的意思是使用'elif'而不是附加的'if's? –