+ 1
3rd biggest number return from entered list of numbers
The function takes in input a list of numbers y, and return the 3rd biggest number in y. If len(y) < 5, it returns the biggest len(y) – 1 element. e.g. if len(y) is 3, it returns the 2nd biggest element, and so on. If the list is empty returns None.
2 odpowiedzi
+ 10
What you did is good, only that it doesn't consider the empty case. Here's what you can do:
try:
y = [int(x) for x in input("enter a list of numbers: ").split(" ")]
if len(y)==1:
print(y[0])
else:
print(sorted(y,reverse=True)[1])
except:
print(None)
Note that this is not a function but a function would look similar to this.
+ 3
what I have done .
y = [int(x) for x in input("enter a list of numbers:").split()]
print(sorted(y, reverse=True)[2])