Python脚本入门期末项目报错求助:Item与State定义问题
问题修复与解答
一、错误原因及修复
1. 第27行(get_item函数)错误
原代码中房间字典的物品键是小写'item',但函数里写的是大写'Item',触发KeyError;同时Town Square房间没有物品,直接访问不存在的键也会报错。修改后的函数:
def get_item(state): # 用get方法避免键不存在报错,无物品时返回None return rooms[state].get('item')
2. 第45行(获取item)错误
Town Square没有物品,原代码直接打印会显示You see a None,需添加判断逻辑:
item = get_item(state) if item: print('You see a', item) else: print('There is no item here.')
3. 其他隐藏逻辑错误
Tailor房间的East方向值带前导空格:' Town Square',导致移动后找不到房间,改为'Town Square'Chapel房间的South方向指向自身:'Chapel',逻辑错误,改为'Jeweler'(对应Jeweler的North是Chapel)
二、库存管理疑问
不需要创建新字典,当前使用的inventory列表完全满足需求。但需优化逻辑:拿到物品后从原房间移除该物品,避免回到房间重复显示物品。修改获取物品的代码段:
elif item and direction == f'get {item}': if item in inventory: print('Item already taken go to another room!!') else: print(f"{item} retrieved!") inventory.append(item) # 从房间字典中删除物品,防止重复获取 del rooms[state]['item']
三、完整修改后的代码
# 房间定义 rooms = { 'Town Square': {'North': 'Bookstore', 'South': 'Restaurant', 'East': 'Tailor', 'West': 'Jeweler'}, 'Tailor': {'East': 'Town Square', 'item': 'Suit'}, 'Restaurant': {'North': 'Town Square', 'East': 'Hair Dresser', 'item': 'salad'}, 'Hair Dresser': {'West': 'Restaurant', 'item': 'Comb'}, 'Book Store': {'South': 'Town Square', 'East': 'Shoemaker', 'item': 'Bible'}, 'Shoemaker': {'West': 'Book Store', 'item': 'Shoes'}, 'Jeweler': {'North': 'Chapel', 'West': 'Town Square', 'item': 'Ring'}, 'Chapel': {'South': 'Jeweler', 'item': 'Wife'} } state = 'Town Square' # 获取新房间函数 def get_new_state(state, direction): new_state = state # 直接通过state访问对应房间,无需循环遍历 if direction in rooms[state]: new_state = rooms[state][direction] return new_state # 获取当前房间物品函数 def get_item(state): return rooms[state].get('item') # 显示指令函数 def show_instructions(): print("Welcome to the Wedding Adventure!") print("Collect all 6 items before you reach the chapel, so you will not die of embarrassment when you get left at " "the altar") print("Move commands: go South, go North, go East, go West") print("Add to Inventory: get 'item name'") show_instructions() inventory = [] while True: print('You are in ', state) print('Inventory:', inventory) item = get_item(state) if item: print('You see a', item) else: print('There is no item here.') print('------------------------------') # 未收集全物品到达教堂的失败判定 if state == 'Chapel': print('SLAP ... GAME OVER!') print('Thanks for playing! Hope you enjoyed it!') break direction = input('Enter your move: ') if direction in ['go East', 'go West', 'go North', 'go South']: direction = direction[3:] new_state = get_new_state(state, direction) if new_state == state: print('The room has wall in that direction enter other direction!') else: state = new_state elif item and direction == f'get {item}': if item in inventory: print('Item already taken go to another room!!') else: print(f"{item} retrieved!") inventory.append(item) del rooms[state]['item'] else: print('Invalid direction!!') # 收集全物品的胜利判定 if len(inventory) == 6: print('Congratulations! You have collected all items and got married!') print('Thanks for playing! Hope you enjoyed it!') break
额外优化:将while (1)改为更符合Python习惯的while True,简化get_new_state函数的逻辑,无需循环所有房间。
内容的提问来源于stack exchange,提问作者Joshua
相关产品推荐
相关产品推荐

