如何使用无法停止的循环修复此代码?
How can I fix this code with unstoppable loop?
我打算做一个有趣的小测验,随机 3 个问题,并给出 3 次猜测的机会。
在我尝试执行后,我尝试了正确的答案,但一直循环,并告诉我答案不正确,无法停止循环,我不知道为什么....
谁能帮我看看?
q1 = 'What can one catch that is not thrown?'
a1 = 'A cold'
q2 = 'If you had only one match and entered a dark room containing an oil lamp, some kindling wood, and a newspaper, which would you light first?'
a2= 'The match'
q3 = 'Some months have 31 days, others have 30 days, but how many have 28 days?'
a3= 'All the months'
import random
quizzes = [q1, q2, q3] #catagorise to quizzes
for answer in random.choice(quizzes):
guesses = 3 # limits to 3 guesses
random.choice(quizzes)
asking = input('Your Answer is?\n')
if quizzes == q1 and asking == 'A cold':
print( 'Bingo!!')
break
elif quizzes == q2 and asking == 'The match':
print( 'Bingo!!')
break
elif quizzes == q3 and asking == 'All the months':
print( 'Bingo!!')
break
elif guesses == 0:
print( 'Sorry, you are reached the maximum guesses. Bye~now~')
else:
guesses -= 1 #reducing the max. guesses of 3 to 2 to 1
print( "Sorry, it's incorrect.")
result = asking
你的问题是猜测的位置。现在它在你的 for 循环中。把它放在你的 for 循环之外,它应该可以工作。
你也应该看看你的
elif guesses == 0:
现在玩家即使猜0次也能猜到。它可能应该是 <=1。
您的代码将按您希望的方式运行:
q1 = 'What can one catch that is not thrown?'
a1 = 'A cold'
q2 = 'If you had only one match and entered a dark room containing an oil lamp, some kindling wood, and a newspaper, which would you light first?'
a2= 'The match'
q3 = 'Some months have 31 days, others have 30 days, but how many have 28 days?'
a3= 'All the months'
import random
quizzes = [q1, q2, q3] #catagorise to quizzes
guesses = 3
for answer in random.choice(quizzes):
# limits to 3 guesses
random.choice(quizzes)
asking = input('Your Answer is?\n')
if quizzes == q1 and asking == 'A cold':
print( 'Bingo!!')
break
elif quizzes == q2 and asking == 'The match':
print( 'Bingo!!')
break
elif quizzes == q3 and asking == 'All the months':
print( 'Bingo!!')
break
elif guesses <=1:
print( 'Sorry, you are reached the maximum guesses. Bye~now~')
exit()
else:
guesses -= 1 #reducing the max. guesses of 3 to 2 to 1
print( "Sorry, it's incorrect.")
result = asking
for answer in random.choice(quizzes)
行实际上是获取一个随机的问题字符串,然后遍历字符串中的每个字符;这就是循环似乎没有停止的原因,因为它迭代的项目比您预期的要多。
你原来在循环中的那一行,random.choice(quizzes)
,什么都不做; random.choice()
函数 returns 列表中的随机项。如果您没有对返回值做任何事情,比如打印它,那么什么也不会发生。
像 if quizzes == q1 and asking == 'A cold':
这样的原始行将不起作用,因为 quizzes
是您的测验列表,因此检查 quizzes == q1
将始终是 False
。在我的代码中,由于我通过 for quiz in quizzes
循环遍历 quizzes
,这意味着我代码中的 quiz
是来自 quizzes
的测验字符串,所以当我执行 quiz == q1
,正确地将当前 quiz
与 q1
、q2
或 q3
.
进行比较
我注意到您在 a1
、a2
和 a3
中定义了您的答案,因此您可以简化代码并使用它们与 asking
进行比较,例如asking == a1
而不是 asking == 'A cold'
.
import random
quizzes = [q1, q2, q3] #catagorise to quizzes
random.shuffle(quizzes) # randomize the quiz order
for quiz in quizzes: # loop through the list of quizzes
guesses = 3 # limits to 3 guesses
print(quiz) # prompt the user with the quiz question
while True: # keep asking for answers until guesses run out, or the user gets a correct answer
asking = input('Your Answer is?\n')
if quiz == q1 and asking == a1:
print( 'Bingo!!')
break
elif quiz == q2 and asking == a2:
print( 'Bingo!!')
break
elif quiz == q3 and asking == a3:
print( 'Bingo!!')
break
elif guesses == 0:
print( 'Sorry, you are reached the maximum guesses. Bye~now~')
break
else:
guesses -= 1 #reducing the max. guesses of 3 to 2 to 1
print( "Sorry, it's incorrect.")
我打算做一个有趣的小测验,随机 3 个问题,并给出 3 次猜测的机会。
在我尝试执行后,我尝试了正确的答案,但一直循环,并告诉我答案不正确,无法停止循环,我不知道为什么....
谁能帮我看看?
q1 = 'What can one catch that is not thrown?'
a1 = 'A cold'
q2 = 'If you had only one match and entered a dark room containing an oil lamp, some kindling wood, and a newspaper, which would you light first?'
a2= 'The match'
q3 = 'Some months have 31 days, others have 30 days, but how many have 28 days?'
a3= 'All the months'
import random
quizzes = [q1, q2, q3] #catagorise to quizzes
for answer in random.choice(quizzes):
guesses = 3 # limits to 3 guesses
random.choice(quizzes)
asking = input('Your Answer is?\n')
if quizzes == q1 and asking == 'A cold':
print( 'Bingo!!')
break
elif quizzes == q2 and asking == 'The match':
print( 'Bingo!!')
break
elif quizzes == q3 and asking == 'All the months':
print( 'Bingo!!')
break
elif guesses == 0:
print( 'Sorry, you are reached the maximum guesses. Bye~now~')
else:
guesses -= 1 #reducing the max. guesses of 3 to 2 to 1
print( "Sorry, it's incorrect.")
result = asking
你的问题是猜测的位置。现在它在你的 for 循环中。把它放在你的 for 循环之外,它应该可以工作。
你也应该看看你的
elif guesses == 0:
现在玩家即使猜0次也能猜到。它可能应该是 <=1。
您的代码将按您希望的方式运行:
q1 = 'What can one catch that is not thrown?'
a1 = 'A cold'
q2 = 'If you had only one match and entered a dark room containing an oil lamp, some kindling wood, and a newspaper, which would you light first?'
a2= 'The match'
q3 = 'Some months have 31 days, others have 30 days, but how many have 28 days?'
a3= 'All the months'
import random
quizzes = [q1, q2, q3] #catagorise to quizzes
guesses = 3
for answer in random.choice(quizzes):
# limits to 3 guesses
random.choice(quizzes)
asking = input('Your Answer is?\n')
if quizzes == q1 and asking == 'A cold':
print( 'Bingo!!')
break
elif quizzes == q2 and asking == 'The match':
print( 'Bingo!!')
break
elif quizzes == q3 and asking == 'All the months':
print( 'Bingo!!')
break
elif guesses <=1:
print( 'Sorry, you are reached the maximum guesses. Bye~now~')
exit()
else:
guesses -= 1 #reducing the max. guesses of 3 to 2 to 1
print( "Sorry, it's incorrect.")
result = asking
for answer in random.choice(quizzes)
行实际上是获取一个随机的问题字符串,然后遍历字符串中的每个字符;这就是循环似乎没有停止的原因,因为它迭代的项目比您预期的要多。
你原来在循环中的那一行,random.choice(quizzes)
,什么都不做; random.choice()
函数 returns 列表中的随机项。如果您没有对返回值做任何事情,比如打印它,那么什么也不会发生。
像 if quizzes == q1 and asking == 'A cold':
这样的原始行将不起作用,因为 quizzes
是您的测验列表,因此检查 quizzes == q1
将始终是 False
。在我的代码中,由于我通过 for quiz in quizzes
循环遍历 quizzes
,这意味着我代码中的 quiz
是来自 quizzes
的测验字符串,所以当我执行 quiz == q1
,正确地将当前 quiz
与 q1
、q2
或 q3
.
我注意到您在 a1
、a2
和 a3
中定义了您的答案,因此您可以简化代码并使用它们与 asking
进行比较,例如asking == a1
而不是 asking == 'A cold'
.
import random
quizzes = [q1, q2, q3] #catagorise to quizzes
random.shuffle(quizzes) # randomize the quiz order
for quiz in quizzes: # loop through the list of quizzes
guesses = 3 # limits to 3 guesses
print(quiz) # prompt the user with the quiz question
while True: # keep asking for answers until guesses run out, or the user gets a correct answer
asking = input('Your Answer is?\n')
if quiz == q1 and asking == a1:
print( 'Bingo!!')
break
elif quiz == q2 and asking == a2:
print( 'Bingo!!')
break
elif quiz == q3 and asking == a3:
print( 'Bingo!!')
break
elif guesses == 0:
print( 'Sorry, you are reached the maximum guesses. Bye~now~')
break
else:
guesses -= 1 #reducing the max. guesses of 3 to 2 to 1
print( "Sorry, it's incorrect.")