#100DaysOfCoding – Day 05 – Challenge 04 of 05

The challenge for today is inspired by a game called “FizzBuzz” game where from a given range of numbers, in this case from 1 to 100, we will write a program to satisfy these 3 conditions.

  1. When the number is divisible by 3 then instead of printing the number it should print “Fizz”.
  2. When the number is divisible by 5, then instead of printing the number it should print “Buzz”.
  3. And finally, if the number is divisible by both 3 and 5 e.g. 15 then instead of the number it should print “FizzBuzz”

e.g. it might start off like this:

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
....etc

Hint

  1. Remember your answer should start from 1 and go up to and including 100.
  2. Each number/text should be printed on a separate line.

My Solution

Let’s start by creating a range of numbers from 1 to 100, inclusive.

Next, if you start by creating an if statement to capture any numbers divisible by 3 or 5, for instance, 15, whoever comes first will be processed first. So if you say:

From here the number 15 will be replaced by Fizz which is something that we don’t want for any number that is divisible by 3 and 5.

The best way to capture or processed first any number that is divisible by 3 and 5 is to put them on top before looking at any number that is specifically divisible only to 3 OR 5.

So here is the completed Python code:

And here is the partial result:

Until then! Have a great day.

Cheers.

#100DaysOfCoding – Day 05 – Challenge 03 of 05

Instructions

You are going to write a program that calculates the sum of all the even numbers from 1 to 100. Thus, the first even number would be 2 and the last one is 100:

i.e. 2 + 4 + 6 + 8 +10 … + 98 + 100

Important, there should only be 1 print statement in your console output. It should just print the final total and not every step of the calculation.

Hint

  1. There are quite a few ways of solving this problem, but you will need to use the range() function in any of the solutions.

And here is my solution:

And here is the result when you run this code:

#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.

Positive SSL