Tanner Britt Tanner Britt - 5 days ago 4
Python Question

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. My program creates a 500 element long list with a random number range of 1,999. My problem arises with my find_max(numbers) function.

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. I have tried using a for count in range(len(list)): as my condition for the loop with the same results.

OUTPUT:

Please enter your menu option: max

The greatest: 10

LIST(after sorting):

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

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)

Answer

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

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.

Comments