Background Information

For this assignment, you will be editing a script that is half written. You will need to finish the comments section, create a loop, and fix some errors in the code. Your final code should perform a little dice rolling game based on the players' name length, then create a list with 20 random numbers in it. You will also create a conditional statement with a nested while loop that will remove all instances of a specified number from the list.

Part 1: Edit & Complete the script

You will be editing the script Mod3 Template.py. Copy it over to your S:\GISProgramming Module3 Scripts directory from the from the R: GIS Programming Module3 Begin by opening it up in a script editor (e.g. Python Win).

For the dice game loop to work properly, you will need to import the random module. Importing modules, if required, is usually the first line of real code in a script.

1. Import the module for random number generation.

The next line of code creates a list of players who will participate in the dice game. You don't need to edit this line.

The next block of code we will focus on is a for loop that runs the dice game. There are two errors written in the code. You will need to fix both errors for the script to run. Find the errors on your own by trying to run the script Ctrl+R or clicking on the run icon (looks like a little guy running).

2. Correct the two errors in the for loop.

Next, create a while loop that adds 20 random integers between 0 and 10 to a list. Be sure to create a counter variable and an empty list first. After all the numbers are added, the full list should print (only at the very end; not after each number is added). This task should require between 5 and 7 lines of code. 3. Create a while loop that adds 20 random numbers between 0 and 10 to a list, and then prints the full list.

In the final block of code, the script will remove all instances of a certain number from the list. If the specified number is not in the list, the script should first print a message that states this. If the number is in the list, a different message should print, stating how many times the number will be removed. The final list should print at the end.

4. Write the code to remove a certain number from the list, and prints relevant messages.

a. First, assign a new variable to the number you want to remove any integer between 0 and 10). This variable will be used as the argument in the methods and functions you will be using. The actual number being used should only appear here.

b. Use a conditional statement to see if the specified number is in the list. If the Python does not count any instances of the number in the list, it should print a message that says something like, for example: "The list does not contain the number 4."

c. If the above condition is not met, the script should print a message that says how many times the number will be removed from the list. This message should concatenate text with the specified number and the count of that number in the list. e.g. "Removing the number 4 from the list 7 times.

d. After that print statement, create a while loop that removes the number from the list. As long as Python counts more than zero instances of the number in the list, it will continue to remove that number from the list.

e. After the loop and conditional statement, print the final version of the list.

5. Save and rename the script as Mod3 [your first initial and last name).py.

Finally, complete the comments section at the very top of the code. Include your name, the script's name, the date, and a brief description of what the script does (keep it short).

6. Complete the comments section at the top of the script.

7. Before you turn in your assignment, make sure the saved script runs properly.

8. For your blog post, take a screenshot of the results in the interactive window. Do not include any parts of your script/code. For example, here is what my results look like: see image.

Answer the following questions in your process summary. The answers are derived from both the Exercise and Assignment.

Q1: What happens if a required module is not imported into the Python script?

Q2: What is the difference between the for and while loop structure?

Q3: When is indentation used in Python code?

Q4: If the conditions of a conditional statement are not met, what does Python do with that block of code when running the script?

Q5: Explain what the number sign # does in Python, and when to use it.

Code Template

##### Step 1:
##### Import the random module

##### Step 5:
##### Complete the comments section, below
# Script name:
# Author:
# Email:
# Date:
# Description:

# list of player names
players = ['Tom', 'Jane', 'Phillip', 'Louis', 'Julius', 'Mary', 'Claudia', 'Marceline']

##### Step 2:
##### Correct the 2 errors in this loop
for p in players:
x = len(p)
dice = random.randint(0, x*2)
print p + " rolls a " + dice + " out of " + str(x*2)
if dice > X:
print p + " wins!"
elif dice == x:
print p + "gets a tie."
else:
print p + " loses."

##### Step 3:
##### Create loop that adds 20 random numbers between 0 and 10 to a list
##### Don't forget to create a counter and an empty list first
##### When the loop is complete, the full list should print (only at the end, not each time a number is added)


##### Step 4:
##### Create loop that removes an unlucky number from the list generated in step 3
##### IF the unlucky number is not in the list to begin with, the script should print a message that says this.
##### Otherwise, the script should print a message saying how many times the number is being removed from the list,
##### and then remove the number.
##### This will require a while loop, and the .count() method.
##### When the loop is complete, the updated list should print (only at the end, not each time a number is removed)
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.