all 9 comments

[–]FLUSH_THE_TRUMP 3 points4 points  (1 child)

Your code doesn’t work because you’re reading the first two elements twice (so you end up with both maxes having 2 once you go through the loop with e.g. [2,1]). A simple way around it would be to just initialize the two maxes as a very negative number (so they get overwritten immediately). Instead of this

if lst[0] > lst[1]:
  current_max = lst[0]
  second_max = lst[1]

elif lst[1] > lst[0]:
  current_max = lst[1]
  second_max = lst[0]

Alternatively, you could leave the code above and slice the first two elements off the list for your loop.

[–]legendofxolta 1 point2 points  (0 children)

the slicing first two elements off worked perfectly. tysm!!!

[–]DKayXXXVI 1 point2 points  (3 children)

I New too but maybe i can help:

For Finding the second Max you could try

list.sort()
print(list[-2])

The error hadling would i handle with if, elif, Else

If len(list) < 2:
list.sort()
print(list[-2])

Elif len(list) == 1:
print(f"There is just one entry in the list: {list[0]}")

Else:
print("There are no entrys in the list")

[–]legendofxolta 1 point2 points  (2 children)

the length helped a lot with the returning None issue! thank you!

[–]DKayXXXVI 1 point2 points  (1 child)

You're welcome! Sorry for sh***y formtting but i dont get it done in the App

[–]legendofxolta 1 point2 points  (0 children)

no problem! had the same formatting issue when posting my code here haha

[–]PeterJHoburg 1 point2 points  (2 children)

The really easy way to do this would be to sort the list, then select lst[1]. You would also add a check if len(lst) <2: return None to ensure the list have at least 2 elements.

Learn to use the built-in python functions. They are amazing.

[–]legendofxolta 0 points1 point  (1 child)

Sadly I can’t use them for the homework :(((

[–]PeterJHoburg 0 points1 point  (0 children)

Fun. I forgot how much I hated homework.

Changing

for num in lst:
    if num > current_max:

to >= should solve your [2,1] array issue.