gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 07:10:57 25 4
gpt4 key购买 nike

这是我的脚本:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com