python - 非常简单的数学游戏的答案总是错误的 - Python

标签 python math if-statement python-3.x

这是我的脚本:

from random import *
from turtle import *

while True: 
    r1 = randint(1,20)
    r2 = randint(1,20)

    # ... ask the question ... then

    a = textinput("Answer?", "")

    if a == r1 * r2:
        write("Well done", font = ("Comic Sans MS", 30, "bold"))
    else:
        write("WRONG", font = ("Comic Sans MS", 30, "bold"))

游戏非常简单。它选择了太多随机数并要求您将它们相乘。你得到正确的答案你的分数上升。回答错误你的分数下降。当我输入正确答案时,它仍然出现“错误” 我想也许我做的 ifs 不同或者它可能不起作用 因为随机数。有没有人知道我的脚本有什么问题。谢谢:)

最佳答案

r1 * r2 是一个整数。

a 是一个字符串。

a转换为整数并进行比较。

转换为整数很简单:

s = int(a)

但是,可以引发 ValueError 并且必须对其进行处理。

关于python - 非常简单的数学游戏的答案总是错误的 - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11121124/

相关文章:

swift - 如何比较两个字符串以查看其中一个是否大于另一个

python - Queue.asyncio值错误: task_done() called too many times - Coding error or a bug detected?

python - 使用 ElementTree getpath() 动态获取 Xpath

math - 计算沿路径的旋转

math - 快速拒绝: Binary division has 0 remainder?

mysql - 如何在 MYSQL 中使用 if 查询创建虚拟列

带过滤器的 JavaScript if 语句将不起作用

python - 从列表中选择符合特定条件的项目

python - 在 numpy 中获取唯一行位置的更快方法是什么

math - 关于PCA降维的问题