首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我想我有个功能论证问题

我想我有个功能论证问题
EN

Stack Overflow用户
提问于 2022-11-28 03:35:23
回答 1查看 36关注 0票数 -2

在大多数情况下,我的所有代码似乎都运行良好。代码是一个基于文本的游戏。当我收集所有的项目,它功能正确,没有错误。但是,如果我直接到Caslte Black而不收集所有项目,它就会以正确的消息结束,但是我会得到以下错误:

代码语言:javascript
复制
Traceback (most recent call last):
  File "C:\Users....................", line 38, in <module>
    city_item = cities[current_city][0]
KeyError: 'Castle Black'

这是我的密码。

代码语言:javascript
复制
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!")

    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并重新开始游戏时

代码语言:javascript
复制
city_item = cities[current_city][1]

我得到以下错误:

代码语言:javascript
复制
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

我不知道该从这里走哪条路。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-28 03:42:40

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74595719

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档