Gre*_*ory 1 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"))
Run Code Online (Sandbox Code Playgroud)
游戏很简单.它会选择过于随机的数字,并要求您将它们相乘.你得到答案,你的分数上升.得分错误你的分数下降.当我输入正确的答案时,它仍然出现"错误"我想我可能正在做不同的ifs,或者因为随机数而可能无法正常工作.有没有人知道我的脚本有什么问题.谢谢 :)
r1 * r2 是一个整数.
a 是一个字符串.
转换a为整数并进行比较.
转换为整数很简单:
s = int(a)
Run Code Online (Sandbox Code Playgroud)
但是,ValueError可以提出并且必须处理.
| 归档时间: |
|
| 查看次数: |
1034 次 |
| 最近记录: |