在Python中构建基于文本的游戏。

huangapple go评论97阅读模式
英文:

Building a text based game in python

问题

I am new to python and am doing a small text-based game in python. The objective is to be able to move room to room. I keep receiving a key error as python isn't recognizing one of the rooms. After I reach the living room I am then given a key error that says this:

  1. if command in rooms[current_room].keys():
  2. KeyError: 'Living room'
  1. rooms = {
  2. 'Entrance room': {'East': 'Master Bedroom'},
  3. 'Master Bedroom': {'North': 'Family room'},
  4. 'Family room': {'East': 'Dining Room', 'South': 'Master Bedroom'},
  5. 'Dining Room': {'South': 'Living room', 'West': 'Family room'},
  6. 'Living Room': {'South': 'Kitchen'},
  7. 'Kitchen': {'South': 'Secret room', 'West': 'Master bedroom'},
  8. 'Secret Room': {'West': 'Dungeon', 'North': 'Kitchen'},
  9. 'Dungeon': {'East': 'Secret room'}
  10. }
  11. instructions = 'Welcome to the dragon slayer game, reach the dungeon to win. To move type: go North, go East, go West, go South'
  12. directions = ['go North', 'go South', 'go East', 'go West']
  13. print(instructions)
  14. current_room = 'Entrance room'
  15. while True:
  16. if current_room == 'Dungeon':
  17. print('Congratulations! You have reached the Dungeon and defeated the Dragon!')
  18. break
  19. print('You are in the {}.'.format(current_room))
  20. command = input('\nWhat do you want to do? ')
  21. if command in directions:
  22. command = command.split()[1]
  23. if command in rooms[current_room].keys():
  24. current_room = rooms[current_room][command]
  25. print(current_room)
  26. else:
  27. print('You can't go that way!')
  28. elif command == 'exit':
  29. print('Thanks for playing!')
  30. break
  31. else:
  32. print('Invalid input')
英文:

I am new to python and am doing a small text based game in python. The objective is to be able to move room to room. I keep receiving a key error as python isn't recognizing one of the rooms. After I reach the living room I am then given a key error that says this:

  1. if command in rooms[current_room].keys():
  2. KeyError: 'Living room'
  1. rooms = {
  2. 'Entrance room': {'East': 'Master Bedroom'},
  3. 'Master Bedroom': {'North': 'Family room'},
  4. 'Family room': {'East': 'Dining Room', 'South': 'Master Bedroom'},
  5. 'Dining Room': {'South': 'Living room', 'West': 'Family room'},
  6. 'Living Room': {'South': 'Kitchen'},
  7. 'Kitchen': {'South': 'Secret room', 'West': 'Master bedroom'},
  8. 'Secret Room': {'West': 'Dungeon', 'North': 'Kitchen'},
  9. 'Dungeon': {'East': 'Secret room'}
  10. }
  11. instructions = 'Welcome to the dragon slayer game, reach the dungeon to win. To move type: go North, go East, go West, go South'
  12. directions = ['go North', 'go South', 'go East', 'go West']
  13. print(instructions)
  14. current_room = 'Entrance room'
  15. while True:
  16. if current_room == 'Dungeon':
  17. print('Congratulations! You have reached the Dungeon and defeated the Dragon!')
  18. break
  19. print('You are in the {}.'.format(current_room))
  20. command = input('\nWhat do you want to do? ')
  21. if command in directions:
  22. command = command.split()[1]
  23. if command in rooms[current_room].keys():
  24. current_room = rooms[current_room][command]
  25. print(current_room)
  26. else:
  27. print('You cant go that way!')
  28. elif command == 'exit':
  29. print('Thanks for playing!')
  30. break
  31. else:
  32. print('Invalid input')

答案1

得分: 0

Python字典的键是区分大小写的。你正在检查的是'Living room'而不是字典中存在的'Living Room'。请注意,字母'r'应该是大写的。

英文:

Python dictionary keys are case-sensitive. You are checking for 'Living room' instead of 'Living Room' which is present in the dictionary. Note that the letter 'r' should be a capital.

huangapple
  • 本文由 发表于 2023年6月6日 09:25:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76410887.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定