Python文本类GOT游戏参数问题:Castle Black引发的KeyError与TypeError
解决《权力的游戏:凛冬将至》文本游戏的KeyError与TypeError问题
问题原因
- KeyError: 'Castle Black':
Castle Black未被添加到cities字典中,但玩家进入该地点后,代码未终止循环,仍尝试执行cities[current_city][0],导致找不到对应键的错误。 - TypeError: can only concatenate str (not "list") to str:错误地将索引从0改为1,而
cities中每个城市的第二个元素是方向列表(并非字符串物品),字符串拼接时类型不匹配触发错误。
解决方案
在检测到玩家进入Castle Black后,打印失败提示并立即终止循环,避免后续代码执行:
while True: if current_city == "Castle Black": print("You have been defeated by the Night King! The Realm is doomed!") break # 终止循环,不再执行后续代码 # 原有的其他代码保持不变...
修改后的完整代码
print("Welcome to the GOT: Winter is Coming Text Based Game") player_name = input("What is your name Lord Commander? ") print("\nIntroduction:") print(" Sir " + player_name + ", as Lord Commander of the Kingsguard") print(" you are to visit 10 cities across Westeros and Essos, and you MUST") print(" collect the item from each city to win the game and defeat the Night King.") print(" Once you collect all the items you automatically win the game defeating the Night King.") print(" If you confront the Night King without all 10 items, you will perish and") print(" all of Westeros and Essos will be doomed!") print("\nGame Play Directions:") print(" To move in a direction you will be prompted to chose either North, South, East or West") print(" To collect an item, you will be prompted to enter 'Y' for 'YES' or 'N' for 'NO'.") print("\nYou are now ready to begin your quest Lord Commander " + player_name + "!\n\n\n") cities = {"King's Landing": [None, ["North", "South", "East", "West"]], "Casterly Rock": ["The Oathkeeper Sword", ["South", "East"]], "Highgarden": ["A Golden Rose", ["North"]], "Sunspear": ["A Viper", ["North", "East"]], "Great Pyramid Meereen": ["Drogon the Dragon", ["West"]], "Dragonstone": ["Dragon Glass", ["North", "West"]], "Pyke": ["The Iron Fleet", ["East"]], "The Twins": ["A Letter of Passage", ["North", "South", "East", "West"]], "The Eyrie": ["A Falcon", ["South", "West"]], "The Dreadfort": ["Lord Bolton's Army", ["West"]], "Winterfell": ["Ghost the Dyer Wolf", ["South", "East", "West"]] } inventory = [] current_city = "King's Landing" while True: if current_city == "Castle Black": print("You have been defeated by the Night King! The Realm is doomed!") break print("Lord Commander, you are currently in", current_city, ".") city_item = cities[current_city][0] print("The current room has", city_item) if city_item != None: option = input("Do you want collect " + city_item + "? (Y/N): ") .upper() if option in ['Y', 'YES']: inventory.append(city_item) cities[current_city][0] = None print("Collected items: ", inventory) if len(inventory) == 10: print("\nCONGRATULATIONS!") print("You have collected all the items and have defeated the Night King!\n") break direction = input("Which direction do you want to go? (North, South, East, West): ") while direction not in cities[current_city][1]: print("You cannot go that way from " + current_city + ". Please try another direction.") direction = input("Which direction do you want to go? (North, South, East, West): ") if current_city == "King's Landing": if direction == "North": next_city = "The Twins" elif direction == "South": next_city = "Sunspear" elif direction == "East": next_city = "Dragonstone" else: next_city = "Casterly Rock" elif current_city == "The Twins": if direction == "North": next_city = "Winterfell" elif direction == "South": next_city = "King's Landing" elif direction == "East": next_city = "The Eyrie" else: next_city = "Pyke" elif current_city == "Sunspear": if direction == "North": next_city = "King's Landing" else: next_city = "Great Pyramid Meereen" elif current_city == "Great Pyramid Meereen": next_city = "Sunspear" elif current_city == "Casterly Rock": if direction == "South": next_city = "Highgarden" else: next_city = "King's Landing" elif current_city == "Highgarden": next_city = "Casterly Rock" elif current_city == "Dragonstone": if direction == "North": next_city = "The Eyrie" else: next_city = "King's Landing" elif current_city == "The Eyrie": if direction == "South": next_city = "Dragonstone" else: next_city = "The Twins" elif current_city == "Pyke": next_city = "The Twins" elif current_city == "Winterfell": if direction == "South": next_city = "The Twins" elif direction == "East": next_city = "The Dreadfort" else: next_city = "Castle Black" elif current_city == "The Dreadfort": next_city = "Winterfell" current_city = next_city print("My Lord, you have moved to", current_city, ".\n") print("\nThank you for saving the Realm!")
内容的提问来源于stack exchange,提问作者Rich Frank
相关产品推荐
相关产品推荐

