This week we read about python fundamentals including: if,
elif and else; lists, dictionaries and tuples; commonly used methods and
function; loops and Booleans. We then practiced implementing these fundamentals
both in the practice exercises and module 2 lab.
We were assigned 4 tasks: to print our last name from a created
list, to fix errors within a prewritten code set, to populate and empty list
with 20 random integers using a while loop with a counter variable. Then to use
an “if” statement and a while loop to remove an “unlucky” number of our choice
from the previously created list and then reprint the list with this integer
removed.
The first two tasks
went smoothly. I created a string of my name and used the split function to
create a list of individual names. Then I was able to use the index function to
print only my last name. The second task was to correct errors in a provided “for”
loop.
The third task was more complicated, but I feel like I
learned a lot through this process. I began by creating my empty list and then
made sure to import the random module, and began to put together my "while" loop. For my initial code I utilized the “random”
function too soon in the script. This resulted in my output simply making a
list of 20 duplicate integers. The second script I ran worked perfectly and
gave the desired output, but it did not contain a counter variable which was a
requirement of the assignment. I wasn’t quite grasping the concept of a counter
variable and breaking a loop, so my next script ended up being an infinite loop
and crashed the program. Finally, after a bit of re-reading I created a working
script that contained all the desired components, however, my output was printing
each addition to the list individually as a separate list, so I ended up with a giant triangle of 20 lists each with a single additional number. This was because I accidentally put the “print” function indented under
the while loop. By moving it to the left where it belonged and not including it
in the loop, I was finally able to print a single list of 20 randomly generated
integers.
The final task built upon task 3. We were to remove an “unlucky”
number (integer of our choice) from the list generated in the last task. We
were to include an “if” statement to print one of two statements. If the
integer was absent, we were to print that it was not in the list. If it was
present we were to print a statement saying that the integer would be removed
from the list for the total count of times it was included in the list. We were
then to use a “while” loop to remove the integer from the list for the “count”
of times it was present. I initially typed my print statements in my “if/else”
statement with the integer itself in the string but I quickly realized I should
reference the variable (unlucky is what I designated it) as a string within the
code in order to create a code that was more efficient and could be changed by
simply changing the variable if I desired, so I made sure that instead of
typing “9 is not in the list” I would make sure the “9” was actually
str(unlucky). I tested this by changing the variable to another integer and the
code ran smoothly and changed easily along with the newly generated list and
newly chosen integer. I finally was able to print the list with the number
removed and completed the assignment, here is an image depicting my final output followed by a flowchart showing the steps I took to complete the task.

