如何使有条件的while循环重复该过程n次?

我有这个简单的猜数游戏,我想做的是,一旦用户猜到了正确的随机数,我想将试验的计数存储在列表中。假设用户在 9 次试验中找到数字,9 将被存储在一个列表中,但我想运行游戏 3 次,并将这 3 次试验存储在一个列表中,然后得到它的平均值。我遇到问题的部分是,一旦用户在 n 次中找到它,它就会将它放在一个列表中,但不会继续。它在1场比赛后停止。如何让它运行3次?任何帮助,将不胜感激。谢谢!


import random

def 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()


元芳怎么了
浏览 336回答 3
3回答

白衣非少年

以下是您的代码的一些问题:您没有在 while 循环中增加 j 。你应该j+=1在循环中的某个地方。您的最后一个打印语句有一个错位的括号。应该是print(sum(list_trial) / len(list_trial))。最后,假设您正在增加 j,您的 while 循环逻辑 (&nbsp;while my_guess != num and j < num_times) 在第一个有效猜测时退出。将所有这些放在一起:num_times = 3j = 0list_trial = []while j < num_times:&nbsp; &nbsp; my_guess = 0&nbsp; &nbsp; counter = 0&nbsp; &nbsp; num = random.randint(1, 3)&nbsp; &nbsp; while my_guess != num:&nbsp; &nbsp; &nbsp; &nbsp; my_guess = int(input('Make a guess&nbsp; -->&nbsp; &nbsp;'))&nbsp; &nbsp; &nbsp; &nbsp; counter += 1&nbsp; &nbsp; &nbsp; &nbsp; if my_guess < num:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('Too low!')&nbsp; &nbsp; &nbsp; &nbsp; elif my_guess > num:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('Too high!')&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('Finally, you got it !')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('It took you ' + str(counter) + ' tries...')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list_trial.append(counter)&nbsp; &nbsp; j += 1print(list_trial)&nbsp; # prints the number of trials...print(sum(list_trial) / len(list_trial))&nbsp; # prints the average of the trials...

精慕HU

您可以将您拆分while为两个分开的whiles。一个用于检查游戏本身num_times的内部和内部while,如下所示:list_trial = []num_times = 3j = 0while j < num_times:&nbsp; &nbsp; num = random.randint(1, 1000)&nbsp; &nbsp; my_guess = 0&nbsp; &nbsp; counter = 0&nbsp; &nbsp; while my_guess != num:&nbsp; &nbsp; &nbsp; &nbsp; my_guess = int(input('Make a guess&nbsp; -->&nbsp; &nbsp;'))&nbsp; &nbsp; &nbsp; &nbsp; counter += 1&nbsp; &nbsp; &nbsp; &nbsp; if my_guess < num:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('Too low!')&nbsp; &nbsp; &nbsp; &nbsp; elif my_guess > num:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('Too high!')&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('Finally, you got it !')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('It took you ' + str(counter) + ' tries...')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list_trial.append(counter)&nbsp; &nbsp; j += 1print(list_trial)&nbsp; &nbsp;#prints the number of trials...print(sum(list_trial) / len(list_trial))

当年话下

您可以只使用列表的长度作为 while 循环中的检查,那么您根本不需要j变量:import randomlist_trial = []num_times = 3while len(list_trial) < num_times:&nbsp; &nbsp; num = random.randint(1, 1000)&nbsp; &nbsp; my_guess = 0&nbsp; &nbsp; counter = 0&nbsp; &nbsp; while my_guess != num:&nbsp; &nbsp; &nbsp; &nbsp; my_guess = int(input('Make a guess&nbsp; -->&nbsp; &nbsp;'))&nbsp; &nbsp; &nbsp; &nbsp; counter += 1&nbsp; &nbsp; &nbsp; &nbsp; if my_guess < num:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('Too low!')&nbsp; &nbsp; &nbsp; &nbsp; elif my_guess > num:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('Too high!')&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('Finally, you got it !')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('It took you ' + str(counter) + ' tries...')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list_trial.append(counter)print(list_trial)&nbsp; &nbsp;#prints the number of trials...print(sum(list_trial / len(list_trial)))&nbsp; &nbsp;# prints the average of the trials...
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python