第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

我怎樣才能用不可停止的循環(huán)修復(fù)這個(gè)代碼?

我怎樣才能用不可停止的循環(huán)修復(fù)這個(gè)代碼?

白板的微信 2023-09-26 14:15:42
我將隨機(jī)進(jìn)行一個(gè)有趣的測(cè)驗(yàn),包含 3 個(gè)問(wèn)題,并提供 3 次猜測(cè)的機(jī)會(huì)。在我嘗試執(zhí)行后,我嘗試了正確的答案,但繼續(xù)循環(huán)并告訴我的答案不正確并且無(wú)法停止循環(huán),我不知道為什么......有人可以幫我看一下嗎?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]  #catagorise to quizzesfor 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
查看完整描述

2 回答

?
臨摹微笑

TA貢獻(xiàn)1982條經(jīng)驗(yàn) 獲得超2個(gè)贊

該行for answer in random.choice(quizzes)實(shí)際上獲取一個(gè)隨機(jī)測(cè)驗(yàn)字符串,然后迭代字符串中的每個(gè)字符;這就是為什么循環(huán)看起來(lái)沒(méi)有停止,因?yàn)樗捻?xiàng)目比您預(yù)期的要多。


最初在循環(huán)中的行random.choice(quizzes), 不執(zhí)行任何操作;該random.choice()函數(shù)從列表中返回一個(gè)隨機(jī)項(xiàng)目。如果您不對(duì)返回值執(zhí)行任何操作(例如打印它),那么什么也不會(huì)發(fā)生。


您原來(lái)的行if   quizzes == q1 and asking == 'A cold':不會(huì)起作用,因?yàn)閝uizzes是您的測(cè)驗(yàn)列表,所以檢查quizzes == q1將始終是False。在我的代碼中,由于我循環(huán)通過(guò)quizzesvia for quiz in quizzes,這意味著quiz在我的代碼中是來(lái)自 的測(cè)驗(yàn)字符串quizzes,因此當(dāng)我quiz == q1這樣做時(shí),可以正確地將當(dāng)前quiz與q1,q2或進(jìn)行比較q3。


a1我注意到您在、a2和中定義了答案a3,因此您可以簡(jiǎn)化代碼并使用它們進(jìn)行比較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.")


查看完整回答
反對(duì) 回復(fù) 2023-09-26
?
紅糖糍粑

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超6個(gè)贊

你的問(wèn)題是猜測(cè)的位置?,F(xiàn)在它在你的 for 循環(huán)內(nèi)。將其放在 for 循環(huán)之外,它應(yīng)該可以工作。


您還應(yīng)該看看您的


elif guesses == 0:

現(xiàn)在,即使猜測(cè)次數(shù)為 0,玩家也能猜出。它可能應(yīng)該是<=1。


您的代碼將按照您想要的方式運(yùn)行,如下所示:


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


查看完整回答
反對(duì) 回復(fù) 2023-09-26
  • 2 回答
  • 0 關(guān)注
  • 134 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)