pattern_matching.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. match command.split():
  2. # ^ keyword
  3. case ["quit"]:
  4. # ^ keyword
  5. print("Goodbye!")
  6. quit_game()
  7. case ["look"]:
  8. # ^ keyword
  9. current_room.describe()
  10. case ["get", obj]:
  11. # ^ keyword
  12. character.get(obj, current_room)
  13. case ["go", direction]:
  14. # ^ keyword
  15. current_room = current_room.neighbor(direction)
  16. # The rest of your commands go here
  17. match command.split():
  18. # ^ keyword
  19. case ["drop", *objects]:
  20. # ^ keyword
  21. for obj in objects:
  22. character.drop(obj, current_room)
  23. match command.split():
  24. # ^ keyword
  25. case ["quit"]: ... # Code omitted for brevity
  26. case ["go", direction]: pass
  27. case ["drop", *objects]: pass
  28. case _:
  29. print(f"Sorry, I couldn't understand {command!r}")
  30. match command.split():
  31. # ^ keyword
  32. case ["north"] | ["go", "north"]:
  33. # ^ keyword
  34. current_room = current_room.neighbor("north")
  35. case ["get", obj] | ["pick", "up", obj] | ["pick", obj, "up"]:
  36. # ^ keyword
  37. pass
  38. match = 2
  39. # ^ variable
  40. match, a = 2, 3
  41. # ^ variable
  42. match: int = secret
  43. # ^ variable
  44. x, match: str = 2, "hey, what's up?"
  45. # <- variable
  46. # ^ variable
  47. if match := re.fullmatch(r"(-)?(\d+:)?\d?\d:\d\d(\.\d*)?", time, flags=re.ASCII):
  48. # ^ variable
  49. return match