**New course!**Every coder should learn

**Generative AI!**

+ 2

# What is the problem with my logic?

Hello, I trying to write a small program with python 3 to find out the runner up from given number. What is the problem with my code? https://code.sololearn.com/cE2veg4thWGh/?ref=app

19 Answers

+ 5

import math
champ = -math.inf
runner = -math.inf
and your code without any changes.

+ 11

This should work even for negative numbers:
s = '-2 -3 -4'
l = list(map(int, s.split()))
largest = l[0]
second_largest = min(l) # using min() feels like cheating
for n in l[1:]:
if n > largest:
second_largest, largest = largest, n
else:
if n > second_largest:
second_largest = n
print('second largest number:', second_largest)
If you don't want to sort the list, you could also just remove the largest number from the list and find the largest number from the remaining list, which will be the second largest number of the original list.

+ 4

I don't know, how is it supposed to work, but when I give input: "2" #or any single digit it prints -1.
When I give input: "1 2 4" #or any space separated sequence of digits, it gives the second last number.

+ 4

Խաչատուր Դալլաքյան Why? That will result in an error because it'll try to convert spaces to integers. If there aren't any spaces, it will convert each digit into an integer, not the number as a whole. '1234' will be converted to [1, 2, 3, 4].

+ 4

The purpose of his program, as I understood is to find second biggest digit in number. arr = [int(a) for a in n] this line of code just splits number into separate digits. I just thought he wants to do so. Because his code for separated numbers works.

+ 3

What kind of errors do you get?

+ 3

Shifan we are waiting for your response. Please explain once again how do you want program to work. And what is the problem with program.

+ 3

Shifan For what kind of input?

+ 3

You check whether i>champ, when there are numbers that are less then -1, they will go throw your code else part, although they shouldn't. And champ and runner will remain -1 unless positive number appears which is wrong. That's why we change this two variable to minus infinity and regardless in first step values of champ and runner will be change. For better understanding your code won't work for numbers less that 4 for example if you give champ and runner initial value 4.

+ 2

Ok, that might be possible 🤔

+ 2

Anna and Rewa Mathur Thanks for your input :)

+ 2

The problem of your code is that you give your variable initial value -1, your code can work if you gave that variable value - infinity.

+ 1

m = n.split()
arr = map(int,m) change this two lines of code into this one: arr = [int(a) for a in n]

+ 1

I would like to find the second largest number but my program is not work for all inputs..sometimes it fails to find exact answer

+ 1

Seb TheS -2 -3 -4

+ 1

Rewa Mathur I would like to do this without sorting.

+ 1

Hi Խաչատուր Դալլաքյան Thanks again. Here is the complete code and it works fine:
import math
if __name__ == '__main__':
n = input()
m = n.split()
arr = map(int,m)
champ = -math.inf
runner = -math.inf
for i in arr:
if(i > champ):
runner = champ
champ = i
elif(i < champ):
if(runner < i):
runner = i
print(runner)

0

What should be the initial value ? Please put modified code.Thanks

0

Խաչատուր Դալլաքյան Thanks for your input . Could you please explain the logic ?