【问题标题】:Dictionary for rooms and items Problem, Python text-based game房间和物品字典问题,基于 Python 文本的游戏
【发布时间】:2021-04-13 00:34:54
【问题描述】:

我已经完成了大部分工作,但目前我的字典将房间链接在一起,然后将其中的项目链接在一起时遇到了问题。第 21 行(以 'Main Fair ground': {'South': 开头)太长且无效。以下是我到目前为止所做的代码,我将不胜感激任何帮助,因为我是 Python 新手并且有点挣扎。

# Sample function showing the goal of the game and move commands
def show_instructions():
    # print a main menu and the commands
    print("Killer Wolf Text Adventure Game")
    print("Collect 6 items to win the game, or be eaten by the killer Wolf.")
    print("Move commands: go South, go North, go East, go West")
    print("Add to Inventory: get 'item name'")


def player_stat():
    print("-" * 20)
    print('You are in the {}'.format(currentRoom))
    print("-" * 20)


def main():
    pass


rooms = {
    'Main Fair ground': {'South': 'Food stand', 'item' : '1LB of meat', 'North': 'Arcade', 'item' : 'real sword', 'East': 'Corn field', 'item' : 'wolf repellent', 'West': 'Candy shop', 'item' : 'candy'},
    'Security': {'item': 'protective gear', 'East': 'Food stand'},
    'Gift shop': {'item': 'map', 'West': 'Arcade'},
    'Petting area': {'item': 'killer wolf', 'South': 'Corn field'}  # villain
}

currentRoom = 'Main Fair ground'
player_move = ''

while currentRoom != 'Exit':
    player_stat()
    player_move = input('Enter your move:\n').lower()
    if player_move in ['Exit', 'exit']:
        currentRoom = 'Exit'
        print('Play again soon')
        continue
try:
    currentRoom = rooms[currentRoom][player_move]
except Exception:
    print("invalid move")
    continue

if 'Petting' == currentRoom:
    print("Kill the wolf")

我得到的错误信息是:

文件“wolf.py”,第 41 行 继续 ^ SyntaxError: 'continue' 在循环中不正确

这是一个来自龙文字游戏的字典示例,这基本上就是我正在做的,所以这是一个很好的例子。

#A dictionary linking a room to other rooms
#and linking one item for each room except the Start room (Great Hall) and the room containing the villain
rooms = {
   'Great Hall' : { 'South' : 'Bedroom', 'North': 'Dungeon', 'East' : 'Kitchen', 'West' : 'Library' },
   'Bedroom' : { 'North' : 'Great Hall', 'East' : 'Cellar', 'item' : 'Armor' },
   'Cellar' : { 'West' : 'Bedroom', 'item' : 'Helmet' },
   'Dining Room' : { 'South' : 'Kitchen', 'item' : 'Dragon' } #villain
}

玩家应输入命令在房间之间移动或从房间中获取物品(如果存在)。游戏循环应该继续循环,允许玩家移动到不同的房间并获取物品,直到玩家赢得或输掉游戏。请记住,玩家通过在遇到与恶棍的房间之前取回所有物品来赢得游戏。玩家在收集所有物品之前与恶棍一起移动到房间,从而输掉了游戏。请务必在两种可能的情况下为玩家提供输出:赢得和输掉游戏。

【问题讨论】:

  • 你的问题到底是什么?
  • 我需要帮助重新格式化房间和项目的字典。那个 python 代码是无效的,第 21 行太长了,我没有做对。请帮帮我。
  • python dict 中的每个键只能有 1 个。所以你是对的,你的rooms['Main Fair ground'] 嵌套字典中不能有 4 个值键入“项目”。您可以考虑使用与房间相同的主要方向键设置单独的dict 项目。

标签: python scripting computer-science text-based


【解决方案1】:

我不太明白你想做什么,但在示例字典中,看起来每个房间都有一个项目或多个路径依次是房间字典上的键,值是另一个字典键是方向或项目,值是相邻房间的名称或项目的名称。 如果是这样,我认为唯一的问题是在 "Main Fair Ground" 字典中,正如我所说的每个内部字典必须最多有一个 "item" 键,要做到这一点,你应该只在 "Main Fair Ground" 里面放方向和在 "rooms" 字典中添加新条目以添加项目,例如:

rooms = {
    'Main Fair ground': {'South': 'Food stand', 'North': 'Arcade', 'East': 'Corn field', 'West': 'Candy shop'},
    'Food stand': {'item': '1LB of meat', 'North': 'Main Fair ground'},
    'Arcade' : {'item': 'real sword', 'South': 'Main Fair ground'}
    ...
}

如果你想要的是 1 磅的肉是食品摊内的物品,而街机内的真剑应该可以工作。

【讨论】:

    【解决方案2】:

    字典必须具有唯一键,但您在第一个房间中指定了多个“项目”键。您还指定了主要展览场地内的其他几个房间,这些房间在其他地方没有定义。例如。您指定“South”:“Food stand”,但您没有“Food stand”房间。您应该将它们拉出到单独的房间字典中并将项目移入其中,例如:

    rooms = {
        "Main fair ground": {"South": "Food Stand", "North": "Arcade", "East": "Corn field", "West": "Candy Shop"},
        "Food stand": {"item": "1LB of meat"},
        "Arcade": {"item": "real sword"},
        "Corn field": {"item": "wolf repellent"},
        "Candy shop": {"item": "Candy"},
        'Security': {'item': 'protective gear', 'East': 'Food stand'},
        'Gift shop': {'item': 'map', 'West': 'Arcade'},
        'Petting area': {'item': 'killer wolf', 'South': 'Corn field'}
    }
    

    然后大概你想为这些新房间添加一些可能的主要方向,以便有人可以在它们之间和其他地方之间移动。您需要弄清楚您的地图在那里的样子。

    此外,您在 while 循环之外还有一个 continue :

    try:
        currentRoom = rooms[currentRoom][player_move]
    except Exception:
        print("invalid move")
        continue
    

    这个块没有缩进到 while 循环中。第 44 行和第 45 行也不是。您最终将永久卡在 while 循环中而不会到达第 39 行,直到您键入“exit”,此时您将在脚本引发 SyntaxError: "continue" 在循环中不正确.

    你需要像这样缩进:

    while currentRoom != 'Exit':
        player_stat()
        player_move = input('Enter your move:\n').lower()
        if player_move in ['Exit', 'exit']:
            currentRoom = 'Exit'
            print('Play again soon')
            continue
        try:
            currentRoom = rooms[currentRoom][player_move]
        except Exception:
            print("invalid move")
            continue
    
        if 'Petting' == currentRoom:
            print("Kill the wolf")
    

    您还应该重新考虑捕获过于宽泛的异常。在这种情况下它会起作用,但这是不好的做法。您应该确定如果用户键入无效移动并准确捕获该异常,则会实际引发什么异常。在这种情况下,如果玩家的移动与定义的方向不匹配,它将是 KeyError

    制定过于宽泛的例外可能会隐藏代码中的其他错误。捕获Exception 是有时间和地点的,例如,如果您正在编写一个库并且需要捕获所有异常,然后使用额外的上下文重新引发自定义异常,但这可能不是那种情况。

    【讨论】:

      【解决方案3】:

      您的代码有一个不在循环中的continue 语句。我认为您需要缩进 try: 行和之后的行,以便将它们视为在它们上方的 while 循环内。否则,Python 会认为这些行是 after 循环而不是 inside

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-14
        • 2015-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多