首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么这个程序不把玩家的房间移到另一个房间?

为什么这个程序不把玩家的房间移到另一个房间?
EN

Stack Overflow用户
提问于 2022-10-22 14:10:52
回答 1查看 31关注 0票数 0
代码语言:javascript
复制
    def show_instructions():
      if __name__ == "__main__":
        while True:
          print('Dragon Text Game\n')


print('\t\t\tDragon Text Game.')
print('--------------------------------------------------------------------------------------- 
------')
print('Move commands: South, North, East, West')
print('Type "Exit" to end the game.')
print("Let's start with your name:  ")

name = input()
print(f'Good luck, {name}!')

# A dictionary for the simplified dragon text game
# The dictionary links a room to other rooms.

rooms = {
         'Great Hall': {'name': 'Great Hall', 'South': 'Bedroom',
         'text': 'You are in the Great Hall.'},
         'Bedroom': {'name': 'Bedroom', 'North': 'Great Hall', 'East': 'Cellar',
         'text': 'You are in the Bedroom.'},
         'Cellar': {'name': 'Cellar', 'West': 'Bedroom',
         'text': 'You are in the Cellar. Type "Exit" to end the game!'}
         }


directions = ['North', 'South', 'East', 'West']
current_room = rooms['Great Hall']
exit = False

# game loop
while True:
    print(current_room['text'])
    command = input('\nEnter your move: ')
    if command == 'Exit':
        if current_room['name'] == 'Cellar':
            # The game completes when the user enters Cellar Room
            print('Congratulations !!')
            print("You've completed 'Dragon Text Game'.")
        print('Thanks for playing!')
        break
    if command in directions:
        if command in current_room:
            current_room = rooms[current_room[command]]
        else:
            print(f'No door in {command} direction!')
         else:
        print('Invalid move!')

输出:

代码语言:javascript
复制
Dragon Text Game.

移动命令:南,北,东,西类型“出口”结束游戏。让我们从你的名字开始:

祝你好运,瑞布!你在大会堂里。

输入您的移动:南方无效移动!你在大会堂里。

输入您的移动:东方无效移动!你在大会堂里。

输入您的移动:退出无效移动!你在大会堂里。

输入您的移动:

一直在重复。在PyCharm中创建,因此所有缩进、空格等都是正确的,尽管您在上面看到了这些。这应该非常简单。我只需要在任何时候都能离开,从字典打电话到另一个房间。我不知道我做错了什么。谢谢!

EN

回答 1

Stack Overflow用户

发布于 2022-10-22 14:16:57

尝试将directions更改为

代码语言:javascript
复制
directions = ['NORTH', 'SOUTH', 'EAST', 'WEST']

并将if command in directions:改为if command.upper() in directions:

您的代码当前无法工作,因为它区分大小写。如果您进入,South而不是south,您很可能可以从一个房间移动到另一个房间。通过强制所有输入都大写,可以避免此问题。为检查if command.upper() == 'EXIT'执行类似的操作

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74164274

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档