循环停止列表

时间:2016-11-28 19:17:54

标签: python loops

我目前正在做一个项目,要求我在没有任何内置模块的帮助下创建一些内置模块的Pythons。我的程序创建了一个500个元素的长列表,随机数范围为1,999。我的问题出现在我的find_max(数字)函数上。

def find_max(numbers):
    i = 0
    stop = 0
    a = i+1
    count = 0
    if numbers[1] > numbers[2]:
            greatest = numbers[1]
    elif numbers[2] > numbers[1]:
            greatest = numbers[2]
    while count != 500:
            if greatest > numbers[i]:
                    i += 1
                    count += 1
            elif numbers[i] > greatest:
                    greatest = numbers[i]
                    i += 1
                    count += 1
            else:
                    count+=1
    print("The greatest: "+str(greatest))
    mainmenu(numbers)

当我在排序列表之前或之后使用它时,它将始终停在第三个元素上。我的find_min(数字)函数是max的镜像副本,其中< intstead of>并始终设法在排序列表之前或之后找到最低值。我已经尝试在范围内使用for count(len(list)):作为循环的条件,结果相同。

输出:

请输入您的菜单选项:max

最伟大的:10

LIST(排序后):

请输入您的菜单选项:排序

[1,7,10,11,12,13,13,14,20,29,34,38,38,39,41,44,45,51,55,56,57,57,57, 62,63,69,72,73,77,78,82,83,83,95,96,98,100,102,103,104,105,106,106,111,114,114,115,116, 117,123,123,125,126,127,132,134,134,135,136,138,139,140,​​140,​​142,142,142,146,148,149,151,154,156,158, 158,161,163,166,166,168,170,173,173,175,175,176,182,183,184,190,197,197,204,204,205,207,207,207,213, 216,217,217,219,219,221,221,221,221,222,225,227,230,233,236,243,244,249,250,250,250,253,254,256,260, 261,262,265,267,268,275,276,277,278,286,290,293,294,297,299,301,302,304,305,307,308,3030,309,315,318, 319,322,325,332,333,334,337,338,338,341,342,342,343,344,345,347,348,352,354,355,355,355,357,357,359, 362,363,367,368,370,373,374,374,378,385,387,387,391,391,392,393,399,401,403,409,417,418,419,419,421, 421,424,424,428,428,430,431,432,432, 434,436,436,436,438,441,441,446,447,449,450,450,452,454,456,457,457,457,457,459,463,464,467,470,470, 471,475,475,476,478,479,484,489,490,501,503,504,504,507,511,513,513,513,516,516,522,532,533,534,534, 537,538,538,540,548,552,557,557,562,563,563,564,572,573,574,576,577,579,583,583,584,587,588,589,592, 592,592,594,595,596,598,601,602,604,605,605,606,606,607,617,617,619,622,622,625,626,627,629,631,634, 636,637,642,642,643,643,653,654,656,658,658,663,669,672,672,673,674,674,679,680,681,682,683,685,692, 695,696,696,698,609,709,709,710,710,711,717,727,727,735,740,741,743,746,754,754,755,757,764,765,765, 768,769,773,773,783,786,788,791,792,792,793,798,800,803,803,809,809,809,813,813,813,814,815,815,816, 817,817,819,821,822,823,824,824,826,827,829,829,830,830,831,831,834,83​​5,839,840,841,841,842,842,842, 843,843,843,844,850,853,854,863,863,864,868,869,870,875,877,879,880,880,882,884,884,886,887,887,889, 893,894,896,896,897,897,899,901,903,903,903,905,906,909,910,913,916,916,917,917,919,920,920,921,922, 922,923,926,926,926,927,928,931,932,932,935,935,936,937,938,943,949,951,951,953,954,956,958,958,959, 960,966,974,974,975,976,978,979,983,984,987,991,993,993,994,995,998,999]

更新:

以下是适用于该问题的代码:

def initial():
    numbers = []
    i = 0
    while i <= 499:
            temp = random.randint(1,999)
            numbers.append(temp)
            i += 1
    return numbers

def find_min(numbers):
    i = 0
    if numbers[1] < numbers[2]:
            least = numbers[1]
    elif numbers[2] < numbers[1]:
            least = numbers[2]
    for element in range(len(numbers)):
            if least < numbers[i]:
                    i += 1
            elif numbers[i] < least:
                    least = numbers[i]
                    i += 1
    print("The lowest: "+str(least))
    mainmenu(numbers)

2 个答案:

答案 0 :(得分:1)

您没有增加列表索引 i 。我发现了一个简单的打印

的问题
while count != len(numbers):
        print (count, i, numbers[i], greatest)
        if greatest > numbers[i]:

这显示了难度,用:

test = [3, 1, 4, 1, 5, 9]
find_max(test)

输出:

0 0 3 4
1 1 1 4
2 2 4 4
3 2 4 4
4 2 4 4
5 2 4 4
The greatest: 4

首先,您的最终 else 子句不会增加 i 。您不需要并行运行两个计数器:使用计数 i ,而不是两者都使用。

其次,既然您事先知道循环的次数,请使用进行循环,而不是进行

for i in range(len(numbers)):

更好,因为你真的不需要知道列表中的 where 最大元素,只需遍历列表(我看到 Mark Tolonen 也是在评论中点击这个:

for element in numbers:

最后,进行此更改将消除 if 语句中的更新计数器,以便您没有所有那些讨厌的冗余代码。

答案 1 :(得分:1)

我在天气方面努力回答这个问题,并可能剥夺你搞清楚它的价值。但是,我认为看到一种更清洁的方式来做你想要完成的事情也是有价值的。我从看到别人的代码中学到了很多东西。您决定是要使用它还是通过自己的方式工作。

>>> def find_max(numbers):
    greatest = numbers[0]
    for x in numbers:
        if x > greatest:
            greatest = x
    return greatest

>>> find_max([3,400,200,100,500,250])
500

>>> find_max([250,500,100,200,400,3])
500

请注意,python会自动逐步执行数字 - 您无需管理增量。在这种情况下,这与您在其他语言中的for-each循环中看到的内容类似,如果您想查找它。

还要注意比较逻辑可以更容易。对于这些类型的事情来说,这是一种非常常见的模式,因此可能值得您熟悉这种方法。