Help me in solving ITSF1901 problem

My issue

why my test are failed?

My code

# cook your dish here
n =list(map(int,input().split()))

largest = n[0]
second_largest=n[0]

for i in range(1,len(n)):
    
    
    if(n[i]>largest):
        lsecond_largest=largest
        largest=n[i]
        
    elif(n[i]<largest):
        
        if(n[i]!=largest and n[i]>second_largest):
            second_largest=n[i]
            
print(second_largest)            

Problem Link: Second largest number Practice Coding Problem

There are a couple of issues in the Python code for finding the second-largest number:

  1. Initialization of second_largest: The initial value of second_largest should not be set to largest. Instead, it should be set to a very small value, such as negative infinity.

  2. Handling edge cases: The current code might fail if there are less than two distinct numbers.

Here’s the corrected version of your code:

n = list(map(int, input().split()))

if len(n) < 2:
print(“There should be at least two numbers.”)
else:
largest = float(‘-inf’)
second_largest = float(‘-inf’)

for i in range(len(n)):
    if n[i] > largest:
        second_largest = largest
        largest = n[i]
    elif n[i] > second_largest and n[i] != largest:
        second_largest = n[i]

if second_largest == float('-inf'):
    print("There is no second largest element.")
else:
    print(second_largest)