【问题标题】:Python text based game基于 Python 文本的游戏
【发布时间】:2021-12-13 00:22:20
【问题描述】:

我正在尝试用 Python 为我的班级的基于文本的游戏编写代码,但遇到了几个问题。首先,由于某种原因,当我尝试调试代码时,它甚至没有运行。当它工作时,每次我输入命令时,它总是会打印多行“输入有效移动”,无论命令是否有效。我在如何从每个房间收集物品并将其添加到库存中也遇到了麻烦。

rooms = {
        'Living Room': {'north': "Parent's Room", 'east': "Kitchen",'south': 'Dining Room', 'west' : 'Office'},
        "Parent's Room": {'east': "Amir's Room", 'south' : 'Living Room', 'item' : 'cape'},
        "Amir's Room": {'west': "Parent's Room"},
        'Office': {'east': 'Living Room', 'item' : 'Toy Phone'}, 
        'Dining Room': {'north': 'Living Room', 'east': 'Garage', 'item': 'Toy Taco'},
        'Garage' : {'west': 'Dining Room', 'item': 'Toy Car'},
        'Kitchen' : {'west': 'Living Room', 'north' : 'Pantry', 'item' : 'Toy Fork'},
        'Pantry' : {'south' : 'Kitchen', 'item' : 'Chocolate'}
    }
direction = '' # Creates empty variable for direction.
location = 'Living Room' # Places user in default starting point
inventory = []

def show_status(): 
    ''' Shows player status and commands.'''

def main():
    # Keep game going until player enters 'exit'
    while True:
        print('----------------------------')
        print('You are in the {}.'.format(location))
        
        input = input('Enter a direction to move: ').strip().lower() # Take input from user to move them in desired direction.
        direction = input.split()

        # Check to see if user entered valid move.
        for command in rooms[location]:
            if direction == command:
                location = rooms[location][direction]
                print(direction)
            elif direction != command: 
                print('Enter a valid move')

【问题讨论】:

    标签: python dictionary


    【解决方案1】:

    其实你的程序有很多问题,让我们快速解决。

    首先,要运行这个程序,你需要在某处调用 main 函数或使用

    if __name__ == "__main__":
        # rest of your program
    

    第二个问题是输入函数从标准输入读取,但在这里:

    input = (
        input("Enter a direction to move: ").strip().lower()
    )  # Take input from user to move them in desired direction.
    

    你覆盖它!您需要将变量重命名为例如用户输入

    下一个问题是

    if direction == command:
    

    如果您认为条件应该满足但不满足,并且您更高级,请尝试学习如何使用调试器。如果没有,print 语句就足够开始了

    print(command) # prints 'north'
    print(direction) # prints ['north']
    

    所以我们发现了另一个问题——确实

    direction = my_input.split()
    

    split() 创建列表,因此您尝试将列表与字符串进行比较,这在这种情况下不起作用。

    并且要在所有房间中旅行,您可以循环使用 dict:

    for room_name, subdict in rooms.items(): #here subdict is a dictionary
        for command, subroom in subdict.items():
            (do what you need)
    

    【讨论】:

    • 老兄,你比我耐心多了哈哈。代码太糟糕了,我不想为他们重写整个脚本。编辑:您错过了一个运行时异常; location 变量超出范围。
    • @KyleNakamura 你知道的,现在是睡眠时间,我们将一切都视为比睡眠更有趣的时间:D 确实代码有一些......不足之处,但希望操作能够进一步发展我们的提示
    【解决方案2】:

    您的代码没有运行,因为从未调用过 main() 函数。

    将此添加到 Python 文件的最后:

    if __name__ == '__main__':
        main()
    

    除此之外……您的代码存在无数运行时错误,导致代码无法按照您的预期工作。举个例子,您的代码有一些问题需要先修复,然后您才能执行其他任何操作:

    1. 添加前面提到的代码调用main()
    2. 通过main() 函数使location 可访问,例如将global location 添加为main() 的第一行。
    3. 重命名您的input 变量,因为您无法重新分配名为input() 的内置函数。例如,将其重命名为 input_str
    4. 修复最后一个 if… else 块内部的逻辑:elif 块不应在每次迭代后打印 “Enter a valid move”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-13
      • 1970-01-01
      • 1970-01-01
      • 2021-06-25
      • 1970-01-01
      • 2013-07-08
      • 2015-09-10
      相关资源
      最近更新 更多