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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使有條件的while循環(huán)重復該過程n次?

如何使有條件的while循環(huán)重復該過程n次?

元芳怎么了 2021-12-29 19:41:27
我有這個簡單的猜數游戲,我想做的是,一旦用戶猜到了正確的隨機數,我想將試驗的計數存儲在列表中。假設用戶在 9 次試驗中找到數字,9 將被存儲在一個列表中,但我想運行游戲 3 次,并將這 3 次試驗存儲在一個列表中,然后得到它的平均值。我遇到問題的部分是,一旦用戶在 n 次中找到它,它就會將它放在一個列表中,但不會繼續(xù)。它在1場比賽后停止。如何讓它運行3次?任何幫助,將不勝感激。謝謝!import randomdef main():    num = random.randint(1, 1000)    my_guess = 0    counter = 0    list_trial = []    num_times = 3    j = 0    while my_guess != num and j < num_times:        my_guess = int(input('Make a guess  -->   '))        counter += 1        if my_guess < num:            print('Too low!')        elif my_guess > num:            print('Too high!')        else:            print('Finally, you got it !')            print('It took you ' + str(counter) + ' tries...')            list_trial.append(counter)    print(list_trial)   #prints the number of trials...    print(sum(list_trial / len(list_trial)))   # prints the average of the trials...main()
查看完整描述

3 回答

?
白衣非少年

TA貢獻1155條經驗 獲得超0個贊

以下是您的代碼的一些問題:

  • 您沒有在 while 循環(huán)中增加 j 。你應該j+=1在循環(huán)中的某個地方。

  • 您的最后一個打印語句有一個錯位的括號。應該是print(sum(list_trial) / len(list_trial))。

  • 最后,假設您正在增加 j,您的 while 循環(huán)邏輯 ( while my_guess != num and j < num_times) 在第一個有效猜測時退出。

將所有這些放在一起:

num_times = 3

j = 0

list_trial = []


while j < num_times:

    my_guess = 0

    counter = 0

    num = random.randint(1, 3)

    while my_guess != num:

        my_guess = int(input('Make a guess  -->   '))

        counter += 1

        if my_guess < num:

            print('Too low!')

        elif my_guess > num:

            print('Too high!')

        else:

            print('Finally, you got it !')

            print('It took you ' + str(counter) + ' tries...')

            list_trial.append(counter)

    j += 1


print(list_trial)  # prints the number of trials...

print(sum(list_trial) / len(list_trial))  # prints the average of the trials...


查看完整回答
反對 回復 2021-12-29
?
精慕HU

TA貢獻1845條經驗 獲得超8個贊

您可以將您拆分while為兩個分開的whiles。一個用于檢查游戲本身num_times的內部和內部while,如下所示:


list_trial = []

num_times = 3

j = 0


while j < num_times:

    num = random.randint(1, 1000)

    my_guess = 0

    counter = 0

    while my_guess != num:

        my_guess = int(input('Make a guess  -->   '))

        counter += 1

        if my_guess < num:

            print('Too low!')

        elif my_guess > num:

            print('Too high!')

        else:

            print('Finally, you got it !')

            print('It took you ' + str(counter) + ' tries...')

            list_trial.append(counter)

    j += 1


print(list_trial)   #prints the number of trials...

print(sum(list_trial) / len(list_trial))


查看完整回答
反對 回復 2021-12-29
?
當年話下

TA貢獻1890條經驗 獲得超9個贊

您可以只使用列表的長度作為 while 循環(huán)中的檢查,那么您根本不需要j變量:


import random


list_trial = []

num_times = 3


while len(list_trial) < num_times:

    num = random.randint(1, 1000)

    my_guess = 0

    counter = 0

    while my_guess != num:

        my_guess = int(input('Make a guess  -->   '))

        counter += 1

        if my_guess < num:

            print('Too low!')

        elif my_guess > num:

            print('Too high!')

        else:

            print('Finally, you got it !')

            print('It took you ' + str(counter) + ' tries...')

            list_trial.append(counter)


print(list_trial)   #prints the number of trials...

print(sum(list_trial / len(list_trial)))   # prints the average of the trials...


查看完整回答
反對 回復 2021-12-29
  • 3 回答
  • 0 關注
  • 282 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號