3 回答

TA貢獻1869條經驗 獲得超4個贊
您的代碼不起作用的原因是因為您只是將兩個數字相乘/除/加/減,而這兩個數字現在聲明為一個字符串。
在 python 中,您不能將字符串作為整數進行加/減/乘/除。您需要將 num1 和 num2 聲明為整數。
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貢獻1841條經驗 獲得超3個贊
您的代碼中有很多語法錯誤,請查看注釋以了解可以改進的地方,底部有一些閱讀材料!
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)
代碼可以很好地適應這些更改,但是您應該閱讀Python 語法和運算符!

TA貢獻1817條經驗 獲得超14個贊
您收到此錯誤是因為默認情況下輸入會給出一個字符串。在使用它之前,您必須將其轉換為 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)
添加回答
舉報