程序不会比较球员答案的真实答案

时间:2015-03-04 10:26:45

标签: variables python-3.x

当我运行程序时,我会输入正确的答案,但它总是标记为错误:

  

(10,'+',2)
  12个
  ...
  错误的答案,答案是12!

以下是错误的代码部分:

Player_answer = input()
print ("...")
time.sleep (0.5)

if operation==("+"): #check answer
    answer = num1+num2  #This works out the real answer

    if Player_answer == answer:  #This works out if the player is correct
        print("That's the correct answer")
        score = score + 1
    else:
        print("Wrong answer, the answer was",answer,"!")

if operation==("*"):
    answer = num1*num2

    if Player_answer == answer:
        print("That's the correct answer")
        score = score + 1
    else:
        print("Wrong answer, the answer was",answer,"!")

elif operation==("-"):
    answer = num1-num2

    if Player_answer == answer:
        print("That's the correct answer")
        score = score + 1
    else:
        print("Wrong answer, the answer was",answer,"!")

1 个答案:

答案 0 :(得分:0)

默认情况下,input()返回一个字符串。因此,即使您输入12,Player_input也是"12"(字符串)。要使Player_input成为int eger,只需输入Player_input = int(Player_input)即可。 int()函数接受任何数据值,并尝试将其转换为整数。但是,如果它无法转换,例如尝试转换"not num",则会引发ValueError为了防止这种情况,您可以将其包装在try/except语句中。