Completed Code for Examples of Data Structures including List, Dictionaries, 2D arrays and Iteration (Big Idea 3).
n/a
grocery_list = []
while True:
action = input("Enter 'add' to add an item, 'remove' to remove an item, or 'display' to show the list: ")
if action == "add":
item = input("Enter an item to add to the list: ")
grocery_list.append(item)
print(f"{item} added to the list!")
elif action == "remove":
item = input("Enter an item to remove from the list: ")
if item in grocery_list:
grocery_list.remove(item)
print(f"{item} removed from the list!")
else:
print(f"{item} not found in the list.")
elif action == "display":
print("Grocery List:")
for item in grocery_list:
print(f"- {item}")
phone_book = {}
while True:
action = input("Enter 'add' to add a contact, 'search' to search for a contact, 'update' to update a contact, or 'exit' to quit: ")
if action == "add":
name = input("Enter a name for the contact: ")
phone = input("Enter a phone number for the contact: ")
phone_book[name] = phone
print(f"{name} added to the phone book!")
elif action == "search":
name = input("Enter a name to search for: ")
if name in phone_book:
phone = phone_book[name]
print(f"{name}: {phone}")
else:
print(f"{name} not found in the phone book.")
elif action == "update":
name = input("Enter a name to update: ")
if name in phone_book:
phone = input("Enter a new phone number: ")
phone_book[name] = phone
print(f"{name}'s phone number updated to {phone}!")
else:
print(f"{name} not found in the phone book.")
elif action == "exit":
break
else:
print("Invalid action. Try again.")
board = [ ['-', '-', '-'],
['-', '-', '-'],
['-', '-', '-']
]
while True:
print("Current Board:")
for row in board:
print(" ".join(row))
row = int(input("Enter a row (0-2): "))
col = int(input("Enter a column (0-2): "))
if board[row][col] != '-':
print("That position is already taken. Try again.")
continue
piece = input("Enter a piece ('X' or 'O'): ")
board[row][col] = piece
# Check for win condition
for i in range(3):
if board[i][0] == board[i][1] == board[i][2] != '-':
print(f"{piece} wins!")
exit()
if board[0][i] == board[1][i] == board[2][i] != '-':
print(f"{piece} wins!")
exit()
if board[0][0] == board[1][1] == board[2][2] != '-':
print(f"{piece} wins!")
exit()
if board[0][2] == board[1][1] == board[2][0] != '-':
print(f"{piece} wins!")
exit()