#100DaysOfCoding – Day 06 – Challenge 01

What’s up? I trust you are well.

Allscan Twelve here sharing with you my #100DaysOfCode challenges and we are now at Day 06 Exercise 01. You may be wondering why I am I’m recording what I’m doing with the course and uploading it to my YouTube Channel? Well, for me, my YouTube Channel is the online video library of my work that I can go back to easily someday if I have to do something similar. This might help you also if you are someone starting to learn Python programming. If you are someone who prefers to read, I do have a duplicate version of this on my blog. Kindly visit my website at allscan12.com. I’ll put the link in the description.

Anyway, Day 06 of the #100DaysOfCode lesson is about Functions, Code Blocks and While Loops. The first exercise on Day 06 of the #100DaysOfCode lesson is about creating a code for our character named “Reeborg” to reach the goal shown with a flag by avoiding obstructions or hurdles since this is a hurdles race.

Let’s first take a look at what are the available commands we can use on Reeborg’s basic keyboard.

Reboorg will start to move forward, then turn left, then move forward, then at this time, Reboorg needs to turn right. Looking back again at the available commands on Reboors’s basic keyboard, you can turn left but there is no turn right command.

So, this is where we need to define a new function to create a turn_right() command.

The syntax for defining a function is def space function name, then open and close parenthesis and now, the final thing, the finishing touch in our function definition, is a colon, because that says, everything that comes after this function, and is indented, belongs with the function.

To create a turn_right command function definition, we can just simply turn_left 3 times.

So, moving on, after turning right, Reeborg’s next step is to move forward, then turn right again, then move forward and finally, turn left.

These whole movements from move() to turn_left() can be assigned to a function called jump.

So, Reeborg’s first move is, basically, to jump and for Reeborg to reach the finish line, Reeborg will do this 6 times.

What if Reboorg has to do this 100 times, is that mean we have to WRITE jump() for 100 times? Well, you can do that if you want but it’s not the best way.

Let’s modify the code and apply what we’ve leant so far in the previous lessons about looping.

So, we can say: for hurdle in range(0, 6) or we can simply write (6) since we have 6 hurdles for Reeborgs to cross.

Then, indented, we can let Reeborg jump. This loops 6 times and by the end of the loop, Reeborg should be at the final or correct position.

You can watch this in action here:

There you have it. Task completed and with shorter lines of code. In this exercise, we’ve learned how to create a function definition by combining all the commands into a single command called turn_left() and jump(). I’m excited to learn the next task where I’ll be learning about the “While” loops.

Have a great day. God bless.

Cheers.

Leave a Reply

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

Positive SSL