#100DaysOfCoding – Day 05 – Challenge 02 of 05

Day 05 Exercise 02 is all about how to find out the maximum value of, say scores or grades, from a list of scores that the user has been provided in the input function.

For this exercise, we are not allowed to use the max() and min() functions.

With every exercise, I always like to upgrade it to the next level and for this one, I’ve added an option for the user to find out not only the high score but with another option to find out the lowest score.

Watch it in action here:

If you prefer reading than watching on my YouTube video version, kindly continue to scroll down.

The image above 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 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.

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.

Here is what I come up with:

Whenever there 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.

I first added a print statement asking the user what do they want to find out.

Then followed by creating a new variable asking the user for some input from given choices.

Then apply what I have leant by using the if,elif and else functions.

To capture any user input that is not 1 or 2, this statement will capture that.

Let’s run the code and see the results.

This time, I’ll intentionally input some random number after specifying the student scores.

Next, let’s make it right by selecting 1 for the highest score.

And by selecting 2 to find out the lowest score, we have this result.

And there you have it. I hope you find the blog post interesting and helpful.

Have a great day.

Cheers.

Leave a Reply

Your email address will not be published. Required fields are marked *

Positive SSL