Exercises E
question I.py
animation_states = ["WALK", "RUN", "JUMP", "STILL", "DUCK"]
### Q1. Append an "ATTACK" state to the animation_states list
### Q2. For each state of animation in the animation_states list:
### remove the state from the list if it contains a "U"
### Q3. Print out the contents of the animation_states list
### output to last question should be:["WALK", "STILL", "ATTACK"]
question II.py
star_wars = {
"Rey": "Apprentice",
"Luke": "Jedi",
"Yoda": "Jedi"
}
# Q1. Add "Anakin" as a key to the star_wars dictionary.
# Anakin's value is "Jedi".
# Q2. Write a script that counts the number of "Jedi" values in the dictionary.
# Your answer to this question (if done correctly) should print: 3
# Q3. Add "C-3PO" and "R2-D2" as keys to the dictionary.
# Both C-3PO and R2-D2's values are "Droid".
# Q4. Modify the script from Q2 so that it sums all values irrespectively.
# The final output should show a tally of each value in the dictionary:
#------------------
# Apprentice: 1
# Jedi: 3
# Droid: 2
question III.py
num = 65 #google keyascii
print(chr(num)) #"A"
print(chr(num+1)) #"B"
### ^^ demonstration can be removed
grid = {}
COLUMNS = 3
ROWS = 3
# Q1. Using the grid dictionary, create the grid values for a game of battleships.
# The dictionary may look like this when populated:
# grid = { "A1": "empty", "A2": "empty", ... "C3": "empty"}
# Q2. If you haven't already, allow the user to change the values for COLUMNS and ROWS
# Q3. Create a 'nice looking' print out of your dictionary in grid format:
# A1 __|A2 __|A3 __|A4 __|A5 __|
# B1 __|B2 __|B3 __|B4 __|B5 __|
# C1 __|C2 __|C3 __|C4 __|C5 __|
# D1 __|D2 __|D3 !!|D4 __|D5 __|
# E1 __|E2 __|E3 __|E4 __|E5 __|
# can you do better?
question IV.py
sample_tuple = ("Newcastle", 12, 9, 17)
# Newcastle had 12 wins, 9 draws, 17 losses.
# Wins are worth +3, draws +1, losses 0.
# Newcastle finished the competition with 45 points.
sample_list = [
sample_tuple,
("West Ham", 15, 7, 16),
]
print( sample_list[0][0] ) # prints Newcastle
print( sample_list[1][3] ) # prints West Ham losses
# ^^ demonstration can be removed
teams = [
("Arsenal", 21, 7, 10),
("Chelsea", 21, 9, 8),
("Liverpool", 30, 7, 1),
("Manchester City", 32, 2, 4),
("Manchester United", 19, 9, 19),
("Tottenham", 23, 2, 13),
]
# Q1. from the teams list of tuples, calculate finishing points for each team.
# Q2. print a list of teams in descending order of their finishing points.
question V.py
game_board = (
["1", "2", "3"],
["4", "5", "6"],
["O", "8", "X"]
)
def display_board():
for row in range(len(game_board)):
line = ""
for col in range(len(game_board[row])):
line += game_board[row][col] + "|"
print(line)
print(len(game_board[row]) * "--")
game_loop = True
while game_loop:
display_board()
pos = int(input("Enter num of position to place X >> "))
# Q1. Continue this code so that it places an X at the chosen location.
# Do not allow me to mark a square that is already taken.
# Q2. Make the game alternate between X and O inputs
# Q3. Stop the game loop when the grid is full
# Q4. After each turn, check for winners (3 in a row) across:
# a) rows (horizontal)
# b) columns (vertical)
# c) diagonals
# If there is a winner, stop the game loop
# Q5 Extension: Make this a single player game,
# So that you are X and the computer is O.
# The computer will randomly choose its own positions.