+ 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

8th Mar 2020, 6:15 AM
Glory222
Glory222 - avatar
15 Answers
+ 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.
8th Mar 2020, 1:09 PM
Tibor Santa
Tibor Santa - avatar
+ 2
biggestNumber = numbers[length-1] Because list index always starts with 0
8th Mar 2020, 6:18 AM
Tibor Santa
Tibor Santa - avatar
+ 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!
8th Mar 2020, 5:01 PM
Glory222
Glory222 - avatar
+ 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.
8th Mar 2020, 5:13 PM
Tibor Santa
Tibor Santa - avatar
+ 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.
8th Mar 2020, 5:20 PM
Tibor Santa
Tibor Santa - avatar
+ 2
ooooooh got i now mate thank you Tibor Santa
8th Mar 2020, 6:10 PM
Glory222
Glory222 - avatar
+ 2
numbers = [6,4,1,8,20,3,7] numbers.sort() print(numbers[-1])
9th Mar 2020, 10:19 AM
Dolan
Dolan - avatar
+ 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 𝚗𝚞𝚖𝚋𝚎𝚛𝚜?
8th Mar 2020, 5:10 PM
Glory222
Glory222 - avatar
+ 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))
9th Mar 2020, 6:48 AM
Sylencer
+ 1
#try numbers = [6,4,1,8,20,3,7] print(max(numbers))
9th Mar 2020, 5:48 PM
Zoe Petrovskaya
Zoe Petrovskaya - avatar
+ 1
hey can i help u?
10th Mar 2020, 2:53 AM
Learner
+ 1
Hello Harshal P. Sonawane I just got it solved by Tibor Santa Bro thanks anyway for trying to help me out
17th Mar 2020, 3:26 PM
Glory222
Glory222 - avatar
+ 1
✌😊
17th Mar 2020, 3:29 PM
Learner
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)
8th Mar 2020, 10:48 AM
Ashleigh Fielders
Ashleigh Fielders - avatar
0
hello mate Tibor Santa i understand that but why does it return an error tho?
8th Mar 2020, 5:18 PM
Glory222
Glory222 - avatar