我怎样才能用不可停止的循环修复这个代码?

我将随机进行一个有趣的测验,包含 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


白板的微信
浏览 67回答 2
2回答

临摹微笑

该行for answer in random.choice(quizzes)实际上获取一个随机测验字符串,然后迭代字符串中的每个字符;这就是为什么循环看起来没有停止,因为它迭代的项目比您预期的要多。最初在循环中的行random.choice(quizzes), 不执行任何操作;该random.choice()函数从列表中返回一个随机项目。如果您不对返回值执行任何操作(例如打印它),那么什么也不会发生。您原来的行if   quizzes == q1 and asking == 'A cold':不会起作用,因为quizzes是您的测验列表,所以检查quizzes == q1将始终是False。在我的代码中,由于我循环通过quizzesvia for quiz in quizzes,这意味着quiz在我的代码中是来自 的测验字符串quizzes,因此当我quiz == q1这样做时,可以正确地将当前quiz与q1,q2或进行比较q3。a1我注意到您在、a2和中定义了答案a3,因此您可以简化代码并使用它们进行比较asking,例如asking == a1代替asking == 'A cold'。import randomquizzes = [q1, q2, q3]  #catagorise to quizzesrandom.shuffle(quizzes)  # randomize the quiz orderfor 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.")

红糖糍粑

你的问题是猜测的位置。现在它在你的 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 randomquizzes = [q1, q2, q3]&nbsp; #catagorise to quizzesguesses = 3&nbsp; &nbsp;&nbsp;for answer in random.choice(quizzes):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# limits to 3 guesses&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;random.choice(quizzes)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;asking = input('Your Answer is?\n')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if&nbsp; &nbsp;quizzes == q1 and asking == 'A cold':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print( 'Bingo!!')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;elif&nbsp; &nbsp;quizzes == q2 and asking == 'The match':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print( 'Bingo!!')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;elif&nbsp; &nbsp;quizzes == q3 and asking == 'All the months':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print( 'Bingo!!')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;elif&nbsp; &nbsp;guesses <=1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print( 'Sorry, you are reached the maximum guesses. Bye~now~')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; guesses -= 1&nbsp; &nbsp; &nbsp; &nbsp; #reducing the max. guesses of 3 to 2 to 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print( "Sorry, it's incorrect.")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result = asking
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python