#100DaysOfCoding

Опубликовано: 14 Октябрь 2024
на канале: Allscan Twelve
1,068
111

#100DaysOfCoding - What is the min/max value in a list - Python Code - Day 5 Challenge #2

What’s up. I trust you are well.

Allscan Twelve is back again for another video sharing with you my solutions to the #100DaysOfCode exercises and in this video, we will be looking at finding out the maximum and minimum student scores from a list of student scores.

Here is the instruction for you to read. If you are interested in learning Python, I’ll put the link to the course in the description.

So, let’s get started coding.

Here is the starting code provided in the class. We have a variable called student scores that is set as an input for the user to fill up. The split() function here at the end of the input statement splits a string into a list. You can specify the separator and by default, the separator is any whitespace. This is the reason why the instruction is to provide space in between scores so you can separate them later.

Next, we have a for loop to convert these scores into a list.
Then we have a print statement that provides you with the output of scores in a list.

Let’s run this to see how this code performs as it is.
Click the run button then input the student scores and make sure to provide space in between scores. So we have 78 65 89 86 55 91 64. Click enter and there you have it, we now have the student scores converted into a list.

This exercise can be solved in just one line of code, or 2 if you want min and max scores.

You simply just say, for maximum score: print(max(student_scores)

And for the minimum score: print(min(student_scores)

Let’s run it, enter the scores with space in between scores, click enter and you’ll have the max and min scores.

But, remember that in this exercise, we are not allowed to use the max or the min function. It would have been easy, as you can see, to calculate the max/min score if we were only allowed to use those functions but we have to learn the hard way to be able to learn the logic on how the looping works.

So, join me as I build the code using the for loop function.

There you have it, challenged completed. It’s time for me to compare my solution to our instructor. For as long as it works, I should be fine with my solution.

Anyway, whenever these are exercises like this, I tend to push it more to a different scenario.

Let’s say I only wanted to see the highest score or the lowest score, not both of them in one output, is it possible? Yes, it is. We just need to modify our code to add these conditions.

Watch how I modify the code to add some conditions using the if, elif and else functions I’ve learnt from the previous lessons. It’s good to use them in practice, otherwise, I might forget how to use them.

So, here we go…

And we are done! I hope you find this video interesting. If you are new to my channel or a returning viewer who can still see that red subscribe button, why don’t you click it now to show support to my YouTube videos?
I would highly recommend that you also click the notification bell so you will get notifications and won’t miss out on my future video uploads.

Have a great day and thank you for watching this far.

Have a great day.

God bless.

Cheers.

Link to the course: https://www.udemy.com/share/103IHM3@m...

Join this channel to get access to perks:
   / @allscantwelve