for i in range(3):
while True:
x = random.randint(10,50)
y = random.randint(10,50)
xy = x*y
print("What's " + str(x) +" * " + str(y) + "?")
a = input()
if int(a) == x*y:
print('YOU ARE CORRECT!')
time.sleep(.5)
else:
print("DISHONOR! THE ANSWER IS ACTUALLY " + str(x*y) + ".")
time.sleep(.5)
print('next')
为什么开头的范围(3)对它下面的代码没有任何影响?
答案 0 :(得分:4)
你在循环中有一个无限循环,你要问的是。因此,外部循环永远不会超出第一次迭代。
您需要完全删除无限循环,或使其有限(通过最终成为True
的条件或使用break
。)
答案 1 :(得分:0)
好的,所以我需要将while修改为以下,这将使循环变为3次。
z = 0
while z < 3:
z = z + 1
x = random.randint(10,50)
y = random.randint(10,50)
xy = x*y
print("What's " + str(x) +" * " + str(y) + "?")
a = input()
if int(a) == x*y:
print('YOU ARE CORRECT!')
time.sleep(.5)
else:
print("DISHONOR! THE ANSWER IS ACTUALLY " + str(x*y) + ".")
time.sleep(.5)
print('Next')
或者,我可以保留原始代码,并添加休息
for i in range(3):
while True:
x = random.randint(10,50)
y = random.randint(10,50)
xy = x*y
print("What's " + str(x) +" * " + str(y) + "?")
a = input()
if int(a) == x*y:
print('YOU ARE CORRECT!')
time.sleep(.5)
break
else:
print("DISHONOR! THE ANSWER IS ACTUALLY " + str(x*y) + ".")
time.sleep(.5)
break
print('next')
或者甚至不同,只是在正确的答案或错误的答案部分添加了一个中断,然后循环将分别在3个正确答案或3个错误答案之后终止(仅对3个答案,无论是对还是错)。