GIS Programming Module 2 – Python Fundamentals
Module 2 assignment covered some
of the fundamentals of Python. This included learning about the different Python
data types, working with string variables, understanding loops and conditional
statements, and iterating through variables.
Step 1 was to create a variable to hold my full name
separated by spaces, split the variable using the spaces, and then print my
last name using the index -1. The index -1 is the last item in the list which
was my last name.
Step 2 was to fix two errors in the code that was
provided. The code uses a list of the first names of players and calculates the
number of characters in players’ name. Each player rolls a dice which is randomly
selected from a list of integers from 0 to 2 times the number of characters in the
name. Players win if the dice is greater than the number of characters, tie if
they are equal, and lose if the dice is less than the number of characters.
The first error in the code was the print statement that
tried to concatenate an integer. To fix the issues, the dice variable needed to
be cast into a string using str(dice).
The second issue was due to Python being case sensitive. The
X in the if statement was uppercase and needed to be lower case to match all
the other x variable references.
Step 3 was to randomly create a list of 20 integers that had values between 0 and 10. The list needed to be created using a while loop and if statement to track the number of numbers added to the list. The last part was to print the list of 20 random numbers.
Step 4 was to choose an unlucky number, count the
number of times the number appeared in the list from step 3, and then print the
number times it would be removed. The last part was to remove the unlucky
number from the list and print the updated list.
Flowchart for all the steps.
Comments
Post a Comment