Lesson 5.7: Adventure Game Pt. 2 – Functions & Input Safety

Today’s Goals

  • Organize adventure game code with functions
  • Reuse code to keep it clean and modular
  • Add input checking to avoid crashes

Warm-Up Question

  • Why do we use functions instead of writing everything in one big block?

Review Example

def intro():
    print("You are in a cave.")
    choice = input("Go left or right? ")
    return choice

def left_path():
    print("You find treasure!")

def right_path():
    print("A monster appears!")

choice = intro()
if choice == "left":
    left_path()
elif choice == "right":
    right_path()
else:
    print("You freeze in fear...")

Class Practice

  • Run the code
  • Add another function for a secret path
  • Why is this easier to read?

Input Safety

  • Use .lower() so input is case-insensitive
  • Provide a default message for invalid input

Example

choice = input("Go left or right? ").lower()
if choice not in ["left", "right"]:
    print("That’s not an option!")

Student Challenge

  • Rewrite your Adventure Game using at least 3 functions
  • Add input safety so unexpected entries don’t break the game

Wrap-Up

  • Functions make your game organized and flexible
  • Next time: Group project work session