简体   繁体   English

循环停止在列表中的第三个元素上

[英]Loop stopping on third element in list

I am currently doing a project that requires me to create some of Pythons built in modules without the aid of any built in modules. 我目前正在做一个项目,要求我在不借助任何内置模块的情况下创建一些内置模块的Python。 My program creates a 500 element long list with a random number range of 1,999. 我的程序创建了一个500个元素的长列表,随机数范围为1,999。 My problem arises with my find_max(numbers) function. 我的问题出在我的find_max(numbers)函数上。

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)

When I use this before or after sorting my list it will always stop on the third element. 当我在对列表进行排序之前或之后使用它时,它将始终停在第三个元素上。 My find_min(numbers) function is a mirror copy of max with < intstead of > and always manages to find the lowest value before or after sorting my list. 我的find_min(numbers)函数是max与<intstead of>的镜像副本,并且始终设法在对列表进行排序之前或之后找到最小值。 I have tried using a for count in range(len(list)): as my condition for the loop with the same results. 我试过在范围(len(list))中使用for计数:作为循环条件,结果相同。

OUTPUT: 输出:

Please enter your menu option: max 请输入菜单选项:最大

The greatest: 10 最大:10

LIST(after sorting): LIST(排序后):

Please enter your menu option: sort 请输入菜单选项:排序

[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, 308, 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, 43 [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,308,309,315,318,319,322 ,325、332、333、334、337、338、338、338、341、342、342、343、344、345、347、348、352、354、355、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、43 4, 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, 699, 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, 835, 839, 840, 841, 841, 842, 842, 842, 84 4,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、534, 537、538、538、540、548、552、557、557、562、563、563、564、572、573、574、576、577、579、583、583、584、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、685、692, 695,696,696,698,699,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、813、814、815、815、816, 817、817、819、821、822、823、824、824、826、827、829、829、830、830、831、831、834、835、839、840、841、841、842、842、842, 84 3, 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] 3,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、905、906、909、910、913、916、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、993、994、995、998、999]

UPDATE: 更新:

Here is the code that applies to the question: 这是适用于该问题的代码:

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)

You didn't increment your list index, i . 您没有增加列表索引, 即i I found the problem with a simple print : 我发现一个简单的打印问题:

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

This shows the difficulty, called with: 这显示了难度,用:

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

Output: 输出:

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

First of all, your final else clause doesn't increment i . 首先,您的最终else子句不会使i递增。 You don't need two counters running in parallel: use either count or i , not both. 您不需要两个并行运行的计数器:使用counti ,而不要同时使用两者。

Second, since you know in advance how many times you go through the loop, use a for loop, not a while : 其次,由于您事先知道要执行多少次循环,因此请使用for循环,而不要花一会儿时间

for i in range(len(numbers)):

Even better, since you don't really need to know where the max element is in the list, just iterate through the list (I see that Mark Tolonen also hit this in a comment): 更妙的是,因为你并不真正需要知道的最大因素是在列表中,只是循环访问列表(我看到马克Tolonen也评论打这个):

for element in numbers:

Finally, making this change will get rid of updating counters within the if statements, so that you don't have all that nasty, redundant code. 最后,进行此更改将摆脱在if语句中更新计数器的麻烦,因此您不必拥有所有讨厌的冗余代码。

I struggle with weather to answer this and potentially deprive you of the value of figuring it out. 我在天气问题上挣扎着回答这个问题,并有可能使您失去解决它的价值。 However, I think that there is also value in seeing a cleaner way of doing what you are trying to accomplish. 但是,我认为看到更干净的方式来完成您要完成的任务也很有价值。 I learn a lot myself from seeing other's code. 我从看别人的代码中学到了很多东西。 You decide if you want to use this or work through your own. 您可以决定要使用此功能还是自己操作。

>>> 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

Notice, that python will automatically step through the numbers - you don't need to manage the increments. 请注意,python将自动浏览数字-您无需管理增量。 In this case, this is similar to what you would see in a for-each loop in other languages if you want to look it up. 在这种情况下,这类似于您要查找时在其他语言的for-each循环中看到的内容。

Also notice how much easier the comparison logic can be. 还要注意比较逻辑可以轻松得多。 This is a pretty common pattern for these sorts of things so its probably worth your getting comfortable with this approach. 对于这类事情,这是一种非常常见的模式,因此您可能需要对此方法感到满意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM