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

#100DaysOfCoding – Day 05 – Challenge 05 of 05

The exercise in today’s challenge is to create a Python code to generate a random password from letters, numbers and symbols.

Let’s start by specifying the letters, numbers and symbols that are mostly accepted by any website when creating passwords.

Then some print statements asking the user for their input in terms of how many characters of each kind they want to use for their password.

Then we need to import the random module which we need to create some random arrangements by saying:

Then I created 3 “for loops” for my letters, symbols and numbers.

Then I combined them all into a single password in the form of letters+symbols+numbers.

and that completes the easy part of this challenge. The next higher level for this challenge is to randomise these characters to make it difficult to hack by any professional hackers.

I haven’t seen the solution from our instructor but after googling, I found out that I can use the function called random.shuffle(). This function doesn’t work with string. It can’t accept a string argument.

The first thing we need to do is to convert the random_password from line 42 to a list. Let’s do that by saying: new_list = list(random_password)

Next, let’s reshuffle it by saying random.shuffle(new_list). I did mine twice here.

Then let’s view the result using the print statement by saying print(new_list).

Finally, time to join back those characters in a list using the join() function

Then let’s view the final result using the print statement by saying print(final_password).

Next, we are ready to run the code.

Here is the sample result of selecting 8 letters, 3 numbers and 5 symbols for the password:

Click the link below to watch this in action:

Until then. Have a great day.

Cheers.

Positive SSL