Module 2
Module two contained a large amount of information about Python syntax and how code is organized concerning variables, string, lists, tuples, Booleans, modules, dictionaries, comments, methods, sets, functions, and loops.
The lab incorporated all of those concepts in four steps that were broken in to parts for each step.
The first step was a very simple straight forward task that asked to assign our full name to a variable in the form of a string and then to split that string. Lastly create a last piece of code that returned the last name in the string.
When first creating the original string, there are no commas separating each name because the entire name is considered one value.
Splitting the string is considered a method and looks something like this.
Note: I could have shortened this code a bit by not assigning a new value to namestring.split(" ")
but I think its good to get the practice in assigning variables.
The second task was more difficult as we analyzed an existing block of code and determined what errors would prevent the code from running. I found three errors which were as follows:
1. Missing import random
2. x is defined but later in the loop x was uppercase X. Python being case sensitive would not recognize this undefined variable because x is not the same thing as X.
3. The print function of a list of variables that was concatenated included an integer. (dice) because python will not allow you to concatenate anything but strings, I had to add a string literal qualifier before dice and add parenthesis which looks like str(dice)
Below is the final product with the return result.
The third step asked us to create a block of code that created a list of twenty variables by running a module called random and is typed by inputting random.randint()
I'd like to add some context to this next bit of code before I explain my process.
This was my first attempt at creating a While Loop in python so the code is very rudimentary and would not likely be written this way in a professional setting.
If I were to write this code today, I would not start while loop with While True:
Instead I would phrase is While len(luckylist) <=20:
This version as opposed to the example below would be shorter and not include and extra if condition within the loop because the interpreter would instead, keep referring back to the original while condition until the condition became false, then the loop would stop at the break. In-fact I may not even need to include an if because the implied loop exit is built into the While condition that the length of the luckylist was less than or equal to 20.
However, I was able to achieve the desired result in the end.
The last step 4 asked us to create a "luckylist" with integers and then to pick a number to be removed from the list from a while loop.
Again as I previously stated, my methods for returning a working result were a bit primitive as I have just started learning Python coding language.
The steps did not exactly call for us to add strings to print like the ones I created but I wanted to add a little bit of flavor to this program to personalize it as I find it challenging.
Assigning variables is very important in any coding language for if you refer to a variable in your script but haven't defined what that is, the interpreter has no idea what you're referencing.
The while loop in plainer terms says :while it is true that num variable (10) is in the luckylist, remove the value 10 from the list. If the statement is no longer true, do something else. The last part is implied in the while loop statement at the beginning of the loop once it becomes False. Tip: colons are a must for loops.
While loops will run infinitely without an exit condition.
All in all my understanding of basic syntax and everything else we learned in module two has grown tremendously as a lot of the growth was achieved through trial and error. I ended up getting stuck with an infinitely looping command a few times but once I started coding in PyCharm, stopping my scripts from running was much easier and quicker.
Comments
Post a Comment