This week we learned some more about the fundamentals of Python. We learned how to import modules into a script, find and correct errors in the code, create loops and conditional statements, add comments, and how to iterate variables within loops.
For this assignment, we were provided a partially written code that we needed to complete. The objective was to create a small dice rolling game based on a players' name length, then create a list containing 20 numbers. We needed to create a conditional statement with a nested while loop that removed all instances of a specified number from that list.
First, we added a line importing the random module. Then we were directed to a couple of lines of code with errors in them that we needed to find and correct. Then the fun part of writing new code began. The next part of code needed to create a loop that generated 20 random integers between 0 and 10 to a list. I started with a counter and an empty list, and used a while loop to accomplish this. The next block of code took some more thought. I wanted to remove a specific number anywhere it turned up in the list. I assigned a new variable and assigned the integer I wanted to remove to that variable. Using a conditional statement and the .count ( ) method, I determined if that specific number was contained in that list. If it did, I used a while loop to find how many times and removed that specific number from the list. Throughout the writing of the program, we were encouraged to use comments, as they not only tell who wrote the script and when, but they also can help describe in plain English what a line or block of code is supposed to accomplish. The screenshot below shows the output of my program. It shows each player's dice roll and whether they won or lost, the 20 integer long list, how many times my specific number was removed, and the list after that integer has been removed.
I really enjoyed working on this lab. It was good to start writing some simple scripts while practicing importing module, using conditional statements and loops, and using comments within the script. I think I may have had an easier time than some due to previous programming experience, but this was an excellent exercise and I still learned a lot about Python specifically. I'm still used to code not being happy with me when I leave an open-ended conditional statement (one without endif), but I am enjoying learning Python and I like it much better than what I have worked with before. I enjoyed working on this script and I look forward to writing more.
No comments:
Post a Comment