Module 2: Introduction to Python Fundamentals

 Hello all, this week in GIS Programming we had an introduction to the basics of writing scripts in/for Python. Below you will see my successful script output for this module in which we created a simulated game of dice rolls where each player rolls a random number, then determines if they win, tie, or lose. Then the second part of the script we generated a list of 20 random integers between 0 and 10 then checked for a specific "unlucky" number, in my case 3 was chosen, the script then removed all instances of it from the list before displaying the final result below. 

Script Results:


The first part is the result from printing my last name, created from the list using my full name. The next part is the results for the dice game simulation from the list of players provided. we were tasked with finding the errors in the original script. The errors I found were that the "random" module was used but never imported, so I added "import random" at the top. Then later in the print statement: p + " rolls a " + dice + " out of " + str(x*2), it was trying to concatenate a string (p + " rolls a ") with an integer (dice) without converting the integer to a string first. This causes a TypeError. The fix for this was to add "str" before (dice).

Next steps were to create a random integer list with 20 randomly generate numbers 0 to 10. I found that if you forget to leave the loop before printing it generated a list 20 times. The fix for this is to make sure you start the print outside the loop.

Then finally we wanted to remove an integer from the list, this integer we deemed "unlucky". We removed the number 3 which occurred 3 times in the generate list. I removed this number by assigning a chosen integer to a variable. Next I checked if the chosen integer is in the list, then used a while loop to remove all instances of the chosen integer. Finally I printed the updated list.

Below are flow charts of my steps.

                                                                  Printing Last Name:
Dice Game:
                

Random Integer List:


Removing Integer from List:




Comments

Popular Posts