Python错误消息:TypeError:'str'对象不可调用[关闭]

num1 = input("Enter your first number: ")
operator = input("Enter +, *, +, or -")
num2 = input("Enter your second number: ")
if operator("+"):
    print(num1 + num2)

对于Python和编码来说是非常新的东西,因此如果错误非常简单,请原谅。

评论
噬魂
噬魂

你需要改变

if operator("+"):
   print(num1 + num2)

if operator == "+":
    print(int(num1) + int(num2))

The operator is a string and to check if a string is equal to a value in python you use ==.
And the num1 and num2 the return from the input as a string so the add them as numbers you need to cast them to int.

点赞
评论