python数学的问题

Rič*_*iņš -3 python math python-3.x

我正在创建一个程序,它将教我的小弟弟数学.但是例如,当程序说2 + 2时,我输入4就说"不正确!".我究竟做错了什么?

import random
import math

def addition():
    num1 = random.randint(1, 100)
    num2 = random.randint(1, 100)
    result = num1 + num2
    guess = input(str(num1) + " + " + str(num2) + " = ")#this is the line with problem
    if guess == result:
        print("Correct!")
    if guess != result:
        print("Incorrect!")
addition()
Run Code Online (Sandbox Code Playgroud)

Mur*_*nik 5

result是一个整数(例如,4),并且输入的猜测是一个字符串(例如,'4').您需要将它们转换为相同的类型才能进行比较.例如:

result = str(num1 + num2)
Run Code Online (Sandbox Code Playgroud)