我认为我有一个函数参数问题



在大多数情况下,我所有的代码似乎工作得很好。代码是一款基于文本的游戏。当我收集所有的项目,它的功能正确无误。但是,如果我直接进入Caslte Black而没有收集所有项目,它会以正确的消息完成,但我得到以下错误:

Traceback (most recent call last):
File "C:Users....................", line 38, in <module>
city_item = cities[current_city][0]
KeyError: 'Castle Black'

这是我的代码。

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 + "!nnn")
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!")
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!")

当我将参数从0改为1并重新开始游戏时

city_item = cities[current_city][1]

我得到以下错误:

Traceback (most recent call last):
File "C:Users............................", line 42, in <module>
option = input("Do you want collect " + city_item + "? (Y/N): ") .upper()
TypeError: can only concatenate str (not "list") to str

我不确定从这里该走哪条路。

在循环中的第一个条件之后,添加break或将其余条件包装在else中。您正在尝试访问cities["Castle Black"],因此访问KeyError

最新更新