Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,030 --> 00:00:02,850
So now that you've gotten a taste for for loops,
2
00:00:03,120 --> 00:00:05,670
it's time to complete another challenge
3
00:00:05,880 --> 00:00:10,880
which is going to stretch you a little bit further. Head over to day 5.2 highest
4
00:00:12,060 --> 00:00:12,893
score,
5
00:00:13,350 --> 00:00:17,640
and we're going to get a list of students scores,
6
00:00:18,210 --> 00:00:20,700
and it's a list of numbers again.
7
00:00:21,390 --> 00:00:23,760
The idea is that you are going to write some code
8
00:00:24,060 --> 00:00:27,690
that's going to output the highest score in the class.
9
00:00:28,050 --> 00:00:30,150
So you're going to look through this list
10
00:00:30,420 --> 00:00:35,420
using a for loop and figure out which of the scores inside the list is the
11
00:00:36,090 --> 00:00:40,470
highest value. Now we can see pretty much at a glance that inside this list,
12
00:00:40,500 --> 00:00:44,670
91 is the highest. But remember that when you test your code,
13
00:00:45,090 --> 00:00:49,200
you won't be able to see the input. So you won't be able to cheat that way.
14
00:00:49,650 --> 00:00:54,480
Instead, you have to do this using code. Now, just as in the last lesson
15
00:00:54,480 --> 00:00:58,560
I mentioned that Python has some pretty useful functions that help us do a lot
16
00:00:58,560 --> 00:01:01,890
of these commonly needed things very easily
17
00:01:02,100 --> 00:01:05,580
such as the sum and the len functions, well, in this case,
18
00:01:05,910 --> 00:01:08,700
there's actually something called the max function.
19
00:01:09,240 --> 00:01:11,400
So if we pass a list,
20
00:01:11,460 --> 00:01:15,810
let's say the student_scores list into a max function
21
00:01:16,290 --> 00:01:21,290
and then I go ahead and print out the maximum value from my list of student_
22
00:01:22,590 --> 00:01:26,700
scores. So let's run this code and I'm going to use the example input,
23
00:01:26,970 --> 00:01:31,500
so a bunch of numbers separated by a space, and hit enter,
24
00:01:31,920 --> 00:01:35,190
and you can see it tells me that 91 is the highest value.
25
00:01:35,760 --> 00:01:38,340
Now there's also the min function
26
00:01:38,820 --> 00:01:43,560
which basically will give me the lowest value inside a particular list,
27
00:01:43,830 --> 00:01:47,850
which in this case is 55, but that's too easy.
28
00:01:48,330 --> 00:01:52,530
I don't want you to use the max or the min functions. Instead,
29
00:01:52,740 --> 00:01:57,210
you have to use what you've learned about for loops to replicate the underlying
30
00:01:57,210 --> 00:02:00,150
functionality of this max function.
31
00:02:00,600 --> 00:02:04,530
Because people tend to use Python to do a lot of data manipulation,
32
00:02:04,710 --> 00:02:07,020
to do a lot of mathematical calculations.
33
00:02:07,260 --> 00:02:09,690
It has a lot of these convenient functions for you.
34
00:02:10,170 --> 00:02:14,130
But let's say you moved to a different language and you work on something else,
35
00:02:14,220 --> 00:02:15,540
then these might not exist.
36
00:02:15,900 --> 00:02:20,070
So understanding the underlying logic and being able to write out the code
37
00:02:20,340 --> 00:02:24,780
using things that every programming language has like the for loop will mean
38
00:02:24,780 --> 00:02:28,440
that you will become a much, much better programmer in the process.
39
00:02:28,980 --> 00:02:33,980
So try and replicate the functionality of the max function and the output should
40
00:02:34,470 --> 00:02:35,340
have the words
41
00:02:35,580 --> 00:02:40,580
The highest score in the class is : and then a space and then the actual max
42
00:02:42,900 --> 00:02:47,010
value. So this is what should happen. Pause the video now,
43
00:02:47,040 --> 00:02:51,510
and have a think about how you would write your code in order to get hold of the
44
00:02:51,510 --> 00:02:53,130
maximum value from a list.
45
00:02:57,990 --> 00:03:01,480
All right. So did you manage to complete this challenge? If not,
46
00:03:01,600 --> 00:03:05,470
and if you want to see how I do it, then continue watching along.
47
00:03:06,610 --> 00:03:10,960
The first thing we want to do is we probably want to get a hold of each of the
48
00:03:10,960 --> 00:03:14,140
scores inside our list of student scores.
49
00:03:14,560 --> 00:03:18,520
So I'm going to write something like for score in student_scores,
50
00:03:19,060 --> 00:03:19,630
so again,
51
00:03:19,630 --> 00:03:24,520
keeping that convention of singular from plural. Now,
52
00:03:24,610 --> 00:03:27,370
inside the loop, what can I do? Now
53
00:03:27,370 --> 00:03:30,280
let's say I had a variable called the highest_score,
54
00:03:30,820 --> 00:03:35,800
and I'm going to start it off as zero. And then inside my loop,
55
00:03:35,950 --> 00:03:40,950
I can use an if statement to check, well is the current score that I'm looping
56
00:03:42,220 --> 00:03:46,540
through greater than the highest score that I've got.
57
00:03:47,200 --> 00:03:51,760
Well then if so, that means that this current score should actually be the
58
00:03:51,760 --> 00:03:52,593
highest score.
59
00:03:52,960 --> 00:03:57,550
So I can set my highest_score to equal,
60
00:03:57,580 --> 00:04:01,210
single equals mean assign, double equals mean checking, remember?
61
00:04:01,900 --> 00:04:06,900
And this loop will continue running until I've checked through all the scores
62
00:04:08,110 --> 00:04:09,910
inside my student_scores,
63
00:04:10,360 --> 00:04:13,780
and I've got the highest score stored inside this variable.
64
00:04:14,290 --> 00:04:18,190
So now I can go ahead and print the sentence;
65
00:04:19,090 --> 00:04:22,930
The highest score is this particular value.
66
00:04:22,990 --> 00:04:27,990
So I'm going to use an f-string to insert my highest_score variable inside here.
67
00:04:30,760 --> 00:04:35,530
And if I run my code and use this example input, then you can see
68
00:04:35,530 --> 00:04:38,410
it tells me the high score in the class is 91,
69
00:04:38,860 --> 00:04:42,790
which matches exactly with the example output. Now,
70
00:04:42,820 --> 00:04:46,360
if you're at all confused about how this code works,
71
00:04:46,660 --> 00:04:50,290
then I recommend again, pasting it into Thonny like this.
72
00:04:50,320 --> 00:04:55,320
So you've got a list of scores and then you've got the code in the solution and
73
00:04:55,450 --> 00:04:56,350
click on debug.
74
00:04:56,560 --> 00:05:01,560
And you can see how we actually step through each of the stages where we start
75
00:05:02,830 --> 00:05:07,240
off with a highest score being equal to zero, so that's where it is,
76
00:05:08,320 --> 00:05:13,320
and then we continue into the loop where we go through each of the scores inside
77
00:05:13,930 --> 00:05:17,890
the student_scores, so the first one is going to be 78.
78
00:05:18,490 --> 00:05:22,960
So now we've got this variable score which is tied to the value of 78.
79
00:05:23,470 --> 00:05:27,100
And then we go and check if the score,
80
00:05:27,640 --> 00:05:32,560
which is 78, is greater than the highest score, which is zero. Well,
81
00:05:32,560 --> 00:05:36,070
that's obviously true, right? So if that's true,
82
00:05:36,070 --> 00:05:38,230
then it's going to carry out the next line of code,
83
00:05:38,620 --> 00:05:42,220
which is that the highest score is now equal to score.
84
00:05:42,910 --> 00:05:47,910
So now notice how this highest_score variable is going to update to the first
85
00:05:49,150 --> 00:05:51,940
value because 78 is greater than zero.
86
00:05:52,480 --> 00:05:55,990
Now we go back to the start of the loop and we go to the next value,
87
00:05:55,990 --> 00:06:00,990
65. And 65 is now checked against the highest score. Is 65 greater than 78?
88
00:06:04,430 --> 00:06:07,340
Well, no, that's not true. So this is false.
89
00:06:07,640 --> 00:06:12,640
And so the highest score is left alone. And we continue back to the next value.
90
00:06:13,130 --> 00:06:18,130
So now our score is equal to 89 and our highest_score is still equal to 78. Is 89
91
00:06:20,750 --> 00:06:22,730
greater than 78? Yes.
92
00:06:22,790 --> 00:06:27,290
So now we reset the highest_score to the new value,
93
00:06:27,620 --> 00:06:28,730
which is 89.
94
00:06:29,030 --> 00:06:34,030
And it continues this loop until it gets to the end and it's found the highest
95
00:06:34,220 --> 00:06:37,430
value and it prints it out into the console.
96
00:06:38,240 --> 00:06:42,410
Play around with the debug and step through each of the stages to see how it
97
00:06:42,410 --> 00:06:46,340
works and compare it with what's happening with each of the variables on the
98
00:06:46,340 --> 00:06:47,810
right. This is a really,
99
00:06:47,810 --> 00:06:51,620
really useful way of wrapping your head around all of this complex,
100
00:06:51,620 --> 00:06:56,480
looping and conditionals. And once you've understood everything that's going on,
101
00:06:56,660 --> 00:07:00,830
then you can continue to the next lesson where we're going to talk about another
102
00:07:00,830 --> 00:07:05,030
type of for loop, the for loop using a range operator.
103
00:07:05,210 --> 00:07:08,000
So for all of that and more, I'll see you in the next lesson.
10223
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.