3 回答

TA貢獻(xiàn)1869條經(jīng)驗(yàn) 獲得超4個(gè)贊
您的代碼不起作用的原因是因?yàn)槟皇菍蓚€(gè)數(shù)字相乘/除/加/減,而這兩個(gè)數(shù)字現(xiàn)在聲明為一個(gè)字符串。
在 python 中,您不能將字符串作為整數(shù)進(jìn)行加/減/乘/除。您需要將 num1 和 num2 聲明為整數(shù)。
num1 = int(input("Enter in your first number"))
num2 = int(input("Enter in your second number"))
sign = input("Enter in the calculator operator you would like")
if sign == "+":
print(num1 + num2)
elif sign == "-":
print(num1 - num2)
elif sign == "*":
print(num1*num2)
elif sign =="/":
print(num1/num2)

TA貢獻(xiàn)1841條經(jīng)驗(yàn) 獲得超3個(gè)贊
您的代碼中有很多語法錯(cuò)誤,請(qǐng)查看注釋以了解可以改進(jìn)的地方,底部有一些閱讀材料!
num1 = int(input("Enter in the first number")) # You need to cast your input to a int, input stores strings.
num2 = int(input("Enter in the second number")) # Same as above, cast as INT
sign = input("Enter in the calculator operator you would like")
# You cannot use `elif` before declaring an `if` statement. Use if first!
if sign == "+": # = will not work, you need to use the == operator to compare values
print(num1 + num2)
elif sign == "-": # = will not work, you need to use the == operator to compare values
print(num1 - num2)
elif sign == "*": # = will not work, you need to use the == operator to compare values
print(num1*num2)
elif sign == "/": # = will not work, you need to use the == operator to compare values
print(num1/num2)
代碼可以很好地適應(yīng)這些更改,但是您應(yīng)該閱讀Python 語法和運(yùn)算符!

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超14個(gè)贊
您收到此錯(cuò)誤是因?yàn)槟J(rèn)情況下輸入會(huì)給出一個(gè)字符串。在使用它之前,您必須將其轉(zhuǎn)換為 int。
num1 = int(input("Enter in the first number"))
num2 = int(input("Enter in the second number"))
sign = input("Enter in the calculator operator you would like")
if sign == "+":
print(num1 + num2)
elif sign == "-":
print(num1 - num2)
elif sign == "*":
print(num1*num2)
elif sign == "/":
print(num1/num2)
添加回答
舉報(bào)