【发布时间】:2015-05-08 06:02:14
【问题描述】:
你好,我正在尝试为随机数学测验生成器编写代码,我有它,所以它随机数字和操作,但我不能让它重复 10 次,因为我希望它问 10 个问题,有人可以在这里帮忙吗是我的代码
import random
import time
name=input("What is your name?")
print ("Alright",name,"Welcome to your maths quiz")
score=0
question=0
finish= False
ops = ['+', '-', '*']
rand=random.randint(1,10)
rand2=random.randint(1,10)
operation = random.choice(ops)
maths = eval(str(rand) + operation + str(rand2))
print ("Your first question is",rand,operation,rand2)
question=question+1
d=int(input ("What is your answer:"))
if d==maths:
print ("Correct")
score=score+1
else:
print ("Incorrect. The actual answer is",maths)
【问题讨论】:
-
我尝试了一个 while 循环,但我不知道我在做什么,因为我对 python 很陌生
-
你在开玩笑吗?你写了这个,但你不知道如何重复它 10 次??
-
你可以使用while循环。
while question <= 10:然后是您的其余代码,然后是您已经完成的question=question+1。 -
for _ in range(5): print('hi').