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

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

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:

if command in rooms[current_room].keys():
KeyError: 'Living room'
rooms = {
        'Entrance room': {'East': 'Master Bedroom'},
        'Master Bedroom': {'North': 'Family room'},
        'Family room': {'East': 'Dining Room', 'South': 'Master Bedroom'},
        'Dining Room': {'South': 'Living room', 'West': 'Family room'},
        'Living Room': {'South': 'Kitchen'},
        'Kitchen': {'South': 'Secret room', 'West': 'Master bedroom'},
        'Secret Room': {'West': 'Dungeon', 'North': 'Kitchen'},
        'Dungeon': {'East': 'Secret room'}
}
instructions = 'Welcome to the dragon slayer game, reach the dungeon to win. To move type: go North, go East, go West, go South'
directions = ['go North', 'go South', 'go East', 'go West']

print(instructions)
current_room = 'Entrance room'

while True:
    if current_room == 'Dungeon':
        print('Congratulations! You have reached the Dungeon and defeated the Dragon!')
        break
    
    print('You are in the {}.'.format(current_room))

    
    command = input('\nWhat do you want to do? ')  
    if command in directions:
        command = command.split()[1]
        if command in rooms[current_room].keys():
            current_room = rooms[current_room][command]
            print(current_room)
        else:
            
            print('You can't go that way!')
    
    elif command == 'exit':
        print('Thanks for playing!')
        break
    
    else:
        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:

if command in rooms[current_room].keys():
KeyError: 'Living room'
rooms = {
        'Entrance room': {'East': 'Master Bedroom'},
        'Master Bedroom': {'North': 'Family room'},
        'Family room': {'East': 'Dining Room', 'South': 'Master Bedroom'},
        'Dining Room': {'South': 'Living room', 'West': 'Family room'},
        'Living Room': {'South': 'Kitchen'},
        'Kitchen': {'South': 'Secret room', 'West': 'Master bedroom'},
        'Secret Room': {'West': 'Dungeon', 'North': 'Kitchen'},
        'Dungeon': {'East': 'Secret room'}
}
instructions = 'Welcome to the dragon slayer game, reach the dungeon to win. To move type: go North, go East, go West, go South'
directions = ['go North', 'go South', 'go East', 'go West']

print(instructions)
current_room = 'Entrance room'

while True:
    if current_room == 'Dungeon':
        print('Congratulations! You have reached the Dungeon and defeated the Dragon!')
        break
    
    print('You are in the {}.'.format(current_room))

    
    command = input('\nWhat do you want to do? ')  
    if command in directions:
        command = command.split()[1]
        if command in rooms[current_room].keys():
            current_room = rooms[current_room][command]
            print(current_room)
        else:
            
            print('You cant go that way!')
    
    elif command == 'exit':
        print('Thanks for playing!')
        break
    
    else:
        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:

确定