#100DaysOfCoding – Day 06 – Challenge 02

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 02. This is just a continuation of our character name “Reeborg” to reach the goal shown with a flag by avoiding obstructions or hurdles, but, this time, with the increase in difficulty.

If you missed the first part, I highly suggest that you read THIS first then come back to this exercise.

So, going back to this second challenge, we know what are the available commands we can use on Reeborg’s basic keyboard. Since there was no turn_right() command, we’ve created a turn_right() function definition and so as the jump() function.

We know from the previous exercise that this code will bring our character Reeborg’s to (13, 1) coordinates. In this next challenge, we never know where is the flag. So, this time, we will be using the “While” loops.

Where do we need to use, “for loop” compared to “while loops”? Good question as I also asked myself that while we were at the beginning of this topic.

“for loops” is used when you have a definite iteration (the number of iterations is known).

For instance, to iterate through a loop with definite range: for hurdle in range(6). With this code, we set and know that this command will run 6 times. It’s gonna stop once it reaches the end of the range.

“While loops”, on the other hand, is an indefinite iteration that is used when a loop repeats an unknown number of times and ends when some condition is met. While loop will continue to run for as long as the condition is true. Having said that, “while loops” are a little bit more dangerous than “for loops”. So, if you have a condition that never will become false, for instance, 2 > 1, 2 will always be greater than one and will never return false, so, your while loop becomes something known as an “Infinite Loop”. This is something that, in most cases, you don’t want to happen to your code. If this happens, don’t worry, force quit the program, restart and try to prevent this from happening in the future.

Sample line of code that will result in an infinite loop

Anyway, let’s go back to Reeborg’s basic keyboard and check what’s on the other tabs. Under “Conditions”, we have these functions. Next, under “Python” we have all of these and we’ve used def(), for in and range() functions as you can see here. Next, we have “Objects”. Maybe we will be using the objects in the next following exercises. And finally under “Specials”, we have these special characters.

Alright. Let’s solve the challenge now using the “while loop”.

Let’s replace all these by while (then, what’s the condition?) at_goal() = False:

We will tell our character Reeborg’s to jump. Let’s try if this will work.

We have an invalid syntax for our while loop. Let’s review again the syntax for the “while loop”. So, we have the while function then space then, a condition that is something true.

Ok, let’s analyze again the problem.

Let’s try another one to see if this will work.

What about while at_goal() != True

The other way of writing line 16 is (from the instructor’s solution):

And it worked!

You can watch it in action here:

There you have it. Task completed.

Have a great day. God bless.

Cheers.

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

Positive SSL