Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1
So now that you've gotten a taste for for loops,
2
it's time to complete another challenge
3
which is going to stretch you a little bit further. Head over to day 5.2 highest
4
score,
5
and we're going to get a list of students scores,
6
and it's a list of numbers again.
7
The idea is that you are going to write some code
8
that's going to output the highest score in the class.
9
So you're going to look through this list
10
using a for loop and figure out which of the scores inside the list is the
11
highest value. Now we can see pretty much at a glance that inside this list,
12
91 is the highest. But remember that when you test your code,
13
you won't be able to see the input. So you won't be able to cheat that way.
14
Instead, you have to do this using code. Now, just as in the last lesson
15
I mentioned that Python has some pretty useful functions that help us do a lot
16
of these commonly needed things very easily
17
such as the sum and the len functions, well, in this case,
18
there's actually something called the max function.
19
So if we pass a list,
20
let's say the student_scores list into a max function
21
and then I go ahead and print out the maximum value from my list of student_
22
scores. So let's run this code and I'm going to use the example input,
23
so a bunch of numbers separated by a space, and hit enter,
24
and you can see it tells me that 91 is the highest value.
25
Now there's also the min function
26
which basically will give me the lowest value inside a particular list,
27
which in this case is 55, but that's too easy.
28
I don't want you to use the max or the min functions. Instead,
29
you have to use what you've learned about for loops to replicate the underlying
30
functionality of this max function.
31
Because people tend to use Python to do a lot of data manipulation,
32
to do a lot of mathematical calculations.
33
It has a lot of these convenient functions for you.
34
But let's say you moved to a different language and you work on something else,
35
then these might not exist.
36
So understanding the underlying logic and being able to write out the code
37
using things that every programming language has like the for loop will mean
38
that you will become a much, much better programmer in the process.
39
So try and replicate the functionality of the max function and the output should
40
have the words
41
The highest score in the class is : and then a space and then the actual max
42
value. So this is what should happen. Pause the video now,
43
and have a think about how you would write your code in order to get hold of the
44
maximum value from a list.
45
All right. So did you manage to complete this challenge? If not,
46
and if you want to see how I do it, then continue watching along.
47
The first thing we want to do is we probably want to get a hold of each of the
48
scores inside our list of student scores.
49
So I'm going to write something like for score in student_scores,
50
so again,
51
keeping that convention of singular from plural. Now,
52
inside the loop, what can I do? Now
53
let's say I had a variable called the highest_score,
54
and I'm going to start it off as zero. And then inside my loop,
55
I can use an if statement to check, well is the current score that I'm looping
56
through greater than the highest score that I've got.
57
Well then if so, that means that this current score should actually be the
58
highest score.
59
So I can set my highest_score to equal,
60
single equals mean assign, double equals mean checking, remember?
61
And this loop will continue running until I've checked through all the scores
62
inside my student_scores,
63
and I've got the highest score stored inside this variable.
64
So now I can go ahead and print the sentence;
65
The highest score is this particular value.
66
So I'm going to use an f-string to insert my highest_score variable inside here.
67
And if I run my code and use this example input, then you can see
68
it tells me the high score in the class is 91,
69
which matches exactly with the example output. Now,
70
if you're at all confused about how this code works,
71
then I recommend again, pasting it into Thonny like this.
72
So you've got a list of scores and then you've got the code in the solution and
73
click on debug.
74
And you can see how we actually step through each of the stages where we start
75
off with a highest score being equal to zero, so that's where it is,
76
and then we continue into the loop where we go through each of the scores inside
77
the student_scores, so the first one is going to be 78.
78
So now we've got this variable score which is tied to the value of 78.
79
And then we go and check if the score,
80
which is 78, is greater than the highest score, which is zero. Well,
81
that's obviously true, right? So if that's true,
82
then it's going to carry out the next line of code,
83
which is that the highest score is now equal to score.
84
So now notice how this highest_score variable is going to update to the first
85
value because 78 is greater than zero.
86
Now we go back to the start of the loop and we go to the next value,
87
65. And 65 is now checked against the highest score. Is 65 greater than 78?
88
Well, no, that's not true. So this is false.
89
And so the highest score is left alone. And we continue back to the next value.
90
So now our score is equal to 89 and our highest_score is still equal to 78. Is 89
91
greater than 78? Yes.
92
So now we reset the highest_score to the new value,
93
which is 89.
94
And it continues this loop until it gets to the end and it's found the highest
95
value and it prints it out into the console.
96
Play around with the debug and step through each of the stages to see how it
97
works and compare it with what's happening with each of the variables on the
98
right. This is a really,
99
really useful way of wrapping your head around all of this complex,
100
looping and conditionals. And once you've understood everything that's going on,
101
then you can continue to the next lesson where we're going to talk about another
102
type of for loop, the for loop using a range operator.
103
So for all of that and more, I'll see you in the next lesson.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.