Well, it was until I hit a SyntaxError that left me completely stuck. I thought it would be something simple maybe a missing parenthesis but it turned out to be a bit more involved than that. If you’re new to Python like me, let me walk you through what happened and how I fixed it.
The Project Idea
I built a game called “Stop the Shot Caller.” The goal is to collect six items a Pistol, Key, Ammo, Kevlar Vest, Flashbang, and Evidence before you enter the room with the SHOT CALLER, or else it’s game over.
Here the original version of the code I wrote:
#Jessica Call
def showInstructions():
print("Stop The Shot Caller ======== COLLECT ALL ITEMS TO WIN. A Shot Caller is waiting for you! Just go South, go North, go East, go West. Collect Six items before you meet the Shot Caller. Find a Pistol, A key, Ammo, a Kevlar Vest, A Flashbang and the Evidence!")
def showStatus(currentRoom, inventory, rooms):
print(' ---------------------------')
print('You are in the ' + currentRoom)
print('Inventory : ' + str(inventory))
if "item" in rooms[currentRoom]:
print('You see a ' + rooms[currentRoom]['item'])
def main():
inventory = []
rooms = {
'Lobby': {'South': 'Basement','North': 'Study', 'East': 'Attic', 'West': 'Kitchen'},
'Basement': {'North': 'Lobby', 'East': 'Security', 'item': 'Flashbang'},
'Elevator': {'West': 'Study', 'item': 'Key'},
'Study': {'South': 'Lobby', 'East': 'Elevator', 'item': 'Pistol'},
'Kitchen': {'West': 'Lobby', 'item': 'Ammo'},
'Attic': {'West': 'Lobby', 'North': 'Parking Lot', 'item': 'Kevlar Vest'},
'Parking Lot': {'South': 'Attic', 'item': 'SHOT CALLER!!!!'},
'Security': {'West': 'Basement', 'item': 'Evidence'}
}
currentRoom = 'IceHall' # Oops! This room doesn't exist!
showInstructions()
while True:
showStatus(currentRoom, inventory, rooms)
if "item" in rooms[currentRoom]:
if rooms[currentRoom]['item'] == 'SHOT CALLER!!!!':
print("DEAD!!!.... GAME OVER!")
break
if len(inventory) == 6:
print("Congratulations! You have collected all items and now have won the game!")
break
print(" ---------------------------")
print("Enter your move: ")
move = ''
while move == '':
move = input('>').lower()
move = move.split()
if len(move) != 2:
print("Invalid Input!")
continue
if move[0] == 'go':
if move[1] in rooms[currentRoom]:
currentRoom = rooms[currentRoom][move[1]]
else:
print("You can't go that way!")
elif move[0] == 'get':
if "item" in rooms[currentRoom] and move[1] in rooms[currentRoom]['item'].lower():
inventory.append(rooms[currentRoom]['item'])
print(rooms[currentRoom]['item'] + ' got!')
del rooms[currentRoom]['item']
else:
print("Can't get " + move[1] + "!")
else:
print("Invalid move!")
if __name__ == '__main__':
main()
The Error I Got
When I tried to run it, Python threw this error:
SyntaxError: invalid syntax
File "main.py", line 58
else:
^^^^
What Went Wrong
Here’s what I learned:
- I had misplaced or badly indented
else:
blocks, which Python does not tolerate. - I had nested a
while
loop unnecessarily inside another, making things more confusing than they had to be. - I also used
'IceHall'
as the starting room but I never defined that room. So when the game tried to look it up in the dictionary, it crashed.
What I Fix
Here are the changes I made:
- Replaced
'IceHall'
with'Lobby'
, which does exist in myrooms
dictionary. - Cleaned up the indentation for all the
if
,elif
, andelse
blocks. - Simplified the move logic.
- Made inputs case-insensitive using
.lower()
. - Prevented items from being picked up multiple times.
def showInstructions():
print("""
Stop The Shot Caller
======================
COLLECT ALL ITEMS TO WIN.
A Shot Caller is waiting for you!
Move commands: go North, go South, go East, go West
Add to Inventory: get 'item name'
Items to collect: Pistol, Key, Ammo, Kevlar Vest, Flashbang, Evidence
""")
def showStatus(currentRoom, inventory, rooms):
print('---------------------------')
print('You are in the ' + currentRoom)
print('Inventory:', inventory)
if "item" in rooms[currentRoom]:
print('You see a ' + rooms[currentRoom]['item'])
def main():
inventory = []
rooms = {
'Lobby': {'South': 'Basement', 'North': 'Study', 'East': 'Attic', 'West': 'Kitchen'},
'Basement': {'North': 'Lobby', 'East': 'Security', 'item': 'Flashbang'},
'Elevator': {'West': 'Study', 'item': 'Key'},
'Study': {'South': 'Lobby', 'East': 'Elevator', 'item': 'Pistol'},
'Kitchen': {'East': 'Lobby', 'item': 'Ammo'},
'Attic': {'West': 'Lobby', 'North': 'Parking Lot', 'item': 'Kevlar Vest'},
'Parking Lot': {'South': 'Attic', 'item': 'SHOT CALLER!!!!'},
'Security': {'West': 'Basement', 'item': 'Evidence'}
}
currentRoom = 'Lobby' # Fixed: Changed from 'IceHall' to valid room
showInstructions()
while True:
showStatus(currentRoom, inventory, rooms)
# Check for loss condition
if "item" in rooms[currentRoom] and rooms[currentRoom]['item'] == 'SHOT CALLER!!!!':
print("You walked into the Parking Lot and faced the SHOT CALLER!")
print(" DEAD!!!.... GAME OVER!")
break
# Check for win condition
if len(inventory) == 6:
print(" Congratulations! You have collected all items and stopped the Shot Caller!")
break
move = input('\nEnter your move: ').lower().split()
if len(move) != 2:
print("Invalid input. Use format: go [direction] or get [item]")
continue
command, target = move
if command == 'go':
if target in rooms[currentRoom]:
currentRoom = rooms[currentRoom][target]
else:
print("You can't go that way!")
elif command == 'get':
if "item" in rooms[currentRoom] and target in rooms[currentRoom]['item'].lower():
if rooms[currentRoom]['item'] not in inventory:
inventory.append(rooms[currentRoom]['item'])
print(f"{rooms[currentRoom]['item']} got!")
del rooms[currentRoom]['item']
else:
print("You already picked that up.")
else:
print("Can't get that item here.")
else:
print("Invalid command. Use 'go' or 'get'.")
if __name__ == '__main__':
main()
Practice Feature I Added
To make the game more interesting and great for practice, I thought of a few bonus ideas you (or future-me) could implement:
- Track the number of moves made before winning or losing.
- Add a
help
command to remind the player how to move or get items. - Randomize the location of items for replayability.
- Let players save their progress or add difficulty levels.
- Show a final score based on how quickly you won or how many items were collected.
Final Thought
This project taught me a lot more than just how to use if
statements. I learned how important structure and indentation are in Python and how easy it is to break things with one wrong line. If you’re a beginner like me, don’t be discouraged by syntax errors. Sometimes it’s just a matter of looking at your code a little more closely or asking for help.