我在 python 中编写了一个小型控制台计算器:
def calculator(num1=0, num2=0):
conta = input("Type operation(+,x,/ or -:)")
if (conta == "+"):
print("Result:" + repr(num1) + " + " + repr(num2) + " is " + str(num1 + num2))
elif (conta == "-"):
print("Result:" + repr(num1) + " - " + repr(num2) + " is " + str(num1 - num2))
elif (conta == "x"):
print("Result:" + repr(num1) + " x " + repr(num2) + " is " + str(num1 * num2))
elif (conta == "/"):
print("Result: " + repr(num1) + " + " + repr(num2) + " is " + str(num1 / num2))
else:
print("Result:" + repr(num1) + conta + repr(num2) + " is Are you Joking?")
try:
num1 = float(input("Type a number:"))
num2 = float(input("Type a number:"))
calculator(num1, num2)
except:
print("Error.")
exit()
它在 IDLE Shell 中正常工作。我放:
500.65 + 300 = 700.65
12 joke 12 = Are you Joking?
然后......当我从 .PY 文件加载它时,它会询问一个数字。我放了。问另一个。放吧。要求手术。我放了一个。窗口关闭。
现在我尝试在 Python 控制台上运行它。回报:
第 1 行中的 SyntaxError => 无效语法。
那么,怎么了?我能做些什么?