List Sim

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}")
bread added to the list!
milk added to the list!
milk removed from the list!
Grocery List:
- bread
Invalid action. Try again.
Invalid action. Try again.
Invalid action. Try again.
Invalid action. Try again.
Invalid action. Try again.
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
/home/amaya/vscode/vscode/fastpages/_notebooks/2023-04-16-workingexamples.ipynb Cell 3 in <cell line: 3>()
      <a href='vscode-notebook-cell://wsl%2Bubuntu/home/amaya/vscode/vscode/fastpages/_notebooks/2023-04-16-workingexamples.ipynb#W2sdnNjb2RlLXJlbW90ZQ%3D%3D?line=0'>1</a> grocery_list = []
      <a href='vscode-notebook-cell://wsl%2Bubuntu/home/amaya/vscode/vscode/fastpages/_notebooks/2023-04-16-workingexamples.ipynb#W2sdnNjb2RlLXJlbW90ZQ%3D%3D?line=1'>2</a> while True:
----> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/amaya/vscode/vscode/fastpages/_notebooks/2023-04-16-workingexamples.ipynb#W2sdnNjb2RlLXJlbW90ZQ%3D%3D?line=2'>3</a>     action = input("Enter 'add' to add an item, 'remove' to remove an item, or 'display' to show the list: ")
      <a href='vscode-notebook-cell://wsl%2Bubuntu/home/amaya/vscode/vscode/fastpages/_notebooks/2023-04-16-workingexamples.ipynb#W2sdnNjb2RlLXJlbW90ZQ%3D%3D?line=3'>4</a>     if action == "add":
      <a href='vscode-notebook-cell://wsl%2Bubuntu/home/amaya/vscode/vscode/fastpages/_notebooks/2023-04-16-workingexamples.ipynb#W2sdnNjb2RlLXJlbW90ZQ%3D%3D?line=4'>5</a>         item = input("Enter an item to add to the list: ")

File ~/anaconda3/lib/python3.9/site-packages/ipykernel/kernelbase.py:1075, in Kernel.raw_input(self, prompt)
   1071 if not self._allow_stdin:
   1072     raise StdinNotImplementedError(
   1073         "raw_input was called, but this frontend does not support input requests."
   1074     )
-> 1075 return self._input_request(
   1076     str(prompt),
   1077     self._parent_ident["shell"],
   1078     self.get_parent("shell"),
   1079     password=False,
   1080 )

File ~/anaconda3/lib/python3.9/site-packages/ipykernel/kernelbase.py:1120, in Kernel._input_request(self, prompt, ident, parent, password)
   1117             break
   1118 except KeyboardInterrupt:
   1119     # re-raise KeyboardInterrupt, to truncate traceback
-> 1120     raise KeyboardInterrupt("Interrupted by user") from None
   1121 except Exception:
   1122     self.log.warning("Invalid Message:", exc_info=True)

KeyboardInterrupt: Interrupted by user

Dictionary Sim

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.")
amay added to the phone book!
amay: 8582679080

2D Array Simulation

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()
Current Board:
- - -
- - -
- - -
Current Board:
- - -
- 2 -
- - -
Current Board:
- - -
- 2 -
-  -
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/home/amaya/vscode/vscode/fastpages/_notebooks/2023-04-16-workingexamples.ipynb Cell 7 in <cell line: 7>()
      <a href='vscode-notebook-cell://wsl%2Bubuntu/home/amaya/vscode/vscode/fastpages/_notebooks/2023-04-16-workingexamples.ipynb#X10sdnNjb2RlLXJlbW90ZQ%3D%3D?line=7'>8</a> for row in board:
      <a href='vscode-notebook-cell://wsl%2Bubuntu/home/amaya/vscode/vscode/fastpages/_notebooks/2023-04-16-workingexamples.ipynb#X10sdnNjb2RlLXJlbW90ZQ%3D%3D?line=8'>9</a>     print(" ".join(row))
---> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/amaya/vscode/vscode/fastpages/_notebooks/2023-04-16-workingexamples.ipynb#X10sdnNjb2RlLXJlbW90ZQ%3D%3D?line=10'>11</a> row = int(input("Enter a row (0-2): "))
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/amaya/vscode/vscode/fastpages/_notebooks/2023-04-16-workingexamples.ipynb#X10sdnNjb2RlLXJlbW90ZQ%3D%3D?line=11'>12</a> col = int(input("Enter a column (0-2): "))
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/amaya/vscode/vscode/fastpages/_notebooks/2023-04-16-workingexamples.ipynb#X10sdnNjb2RlLXJlbW90ZQ%3D%3D?line=13'>14</a> if board[row][col] != '-':

ValueError: invalid literal for int() with base 10: ''