You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python文本游戏移动方向功能异常,请求定位错误代码行

文本类Python游戏移动功能故障排查求助

我是Python初学者(刚上第一节课),这是我的期末项目——一款基于文本的Python游戏。目前游戏的退出和拾取物品功能正常,但移动方向功能存在问题,无法正常运行。我曾尝试修改rooms字典和directions集合的格式来匹配,但仍未解决问题,恳请帮忙定位错误代码行并提供指导。

附游戏代码

rooms={
'Locker Room': {'go South':'Storage Room', 'go West': 'Coaches Office', 'go North': 'Teachers Lounge', 'go East': 'Concession Stand', 'Item': 'gym clothes'},
'Storage Room':{'go North':'Locker Room','go East':'Cafeteria','Item': 'basketballs'},
'Cafeteria':{'go West':'Storage Room', 'Item': 'sports drinks'},
'Concession Stand':{'go West': 'Locker Room', 'go North': 'Gym', 'Item': 'nachos'},
'Coaches Office': {'go East': 'Locker Room', 'Item': 'team films'},
'Teachers Lounge': {'go South': 'Locker Room', 'go East': 'Math Class','Item': 'referees'},
'Math Class': {'go West': 'Teachers Lounge', 'Item': 'teammates'},
'Gym':{'Item' : 'basketball team'}
}
directions: set[str] = {'go North', 'go South', 'go East', 'go West'} #valid moves

def instructions():
    print('\n Welcome to the Battle of the Gym game')
    print('The basketball team has taken over the gym, and your volleyball match is set to begin in one hour.')
    print('You need to defeat the team before the opposing team arrives or forfeit your match.')
    print('But before you go to battle you will need a few items.')
    print('\n You must collect all 7 items to defeat the basketball team.')
    print('\n Move commands: go North, go South, go West, go East')
    print("\n Add to Inventory: get 'item name'")
    print('\n Exit game: exit')
    print('-------------------------------------------------------')
instructions() #prints the game title and how to play
#main loop
def main():
    location = 'Locker Room' #starting place
    inventory = [] #holds inventory

    def show_status(): #where I am, what i have or don't have, or what I need
        print('')
        print(f'You are currently in the {location}')
        print(f'You currently have: ', *inventory, sep=' ')
        if rooms[location]['Item'] in inventory:
           print('This room is empty.')
        else:
            print(f'You see ' + rooms[location]['Item'])

    while True:
        show_status() #prints current room and inventory
        possible_moves = rooms.get(location, {}) #looks for valid moves
        current_item = rooms[location]['Item'] #lets know item
        command = input('\nPlease enter a direction or take the available item: \n').strip().lower() #ask for users move
        if command in directions: #check for valid move
            if command not in rooms[location]: #if not valid command
                print("You can't go that way. Enter a new direction.")
            elif command in possible_moves: #valid move
                location = possible_moves.get(command, location)
                if location == 'Gym': #checks to see if game over
                    if len(inventory) != 7: #if inventory less than 7 game over
                       print("You've been beat! Game Over!")
                       break
                    else: #if inventory is 7 you won
                       print('Congratulations! You have collected all the items and defeated the basketball team!')
                       break
        elif command == 'exit': #exits game
            print("Thank you for playing. Hope you enjoyed the game.")
            break
        elif command == 'get {}'.format(current_item): #add item to inventory
            if current_item not in inventory: #checks inventory and if not in iventory retrieves item
                print('You got ' + current_item,'in your inventory.')
                inventory.append(current_item) #adds to inventory list
            elif current_item in inventory: #already in inventory list tells user that
                print('You already have this item.')
            else: #no items available to end loop
                print('No items available.')
        else: #user enters anything not in directions
            print('Invalid input.')
if __name__ =='__main__':
    main()

我曾尝试修改rooms字典中的方向格式,并调整directions集合内容,但问题仍未解决。

内容的提问来源于stack exchange,提问作者Brittany

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 17:25:57