+ 2
Please,I need help on my python exercise
Hello guys, I want this code to print the largest number given in the list but it somehow gave me some errors that I don't understand! https://code.sololearn.com/cfCDbW4fKbQm/#py
15 Antworten
+ 2
Ashleigh Fielders you are not quite right. The code would work with proper indexing because Glory222 did sort the list, so the last element would be the biggest.
Getting the last element of a list can be done with negative index too:
print(numbers[-1])
You are correct that max(numbers) is the easiest way to determine the maximum.
+ 2
biggestNumber = numbers[length-1]
Because list index always starts with 0
+ 2
Thanks Tibor Santa and Ashleigh Fielders yes I do know that the max would do the job but I was just trying to figure out if i can do it another way!
+ 2
In your program the list has 7 elements. So len(numbers) is 7
When you get an element by index, remember that the first element has index 0 so numbers[0] is the smallest after the sorting, and the last index is 6.
+ 2
Because numbers[7] is invalid, the list has only 7 elements and [7] means the 8th element. So you get index out of bounds error.
+ 2
ooooooh got i now mate thank you Tibor Santa
+ 2
numbers = [6,4,1,8,20,3,7]
numbers.sort()
print(numbers[-1])
+ 1
Hello Tibor Santa, can you explain me why len(numbers)
didn't work since it returns how many elements we have in the list 𝚗𝚞𝚖𝚋𝚎𝚛𝚜?
+ 1
Use ;
Max: for the largest number in the list e.g print(max(list)).
Min:for the smallest number in the given list e. g print (min(list))
+ 1
#try
numbers = [6,4,1,8,20,3,7]
print(max(numbers))
+ 1
hey can i help u?
+ 1
Hello Harshal P. Sonawane I just got it solved by Tibor Santa
Bro thanks anyway for trying to help me out
+ 1
✌😊
0
that wont give you largest number in the list, that will give you the value of the item at index(length of the list).
simply use max(numbers)
0
hello mate Tibor Santa i understand that but why does it return an error tho?