简体   繁体   English

我相信我有一个 Function 争论问题

[英]I believe I have a Function Argument Issue

For the most part all of my code seems to be working fine.在大多数情况下,我的所有代码似乎都运行良好。 The code is a text-based game.该代码是一个基于文本的游戏。 When I collect all of the items it fuctions correctly without errors.当我收集所有项目时,它可以正常运行而不会出错。 But if I go to directly to Caslte Black without collecting all the items it finishes with the proper message, but I get the following error:但是,如果我 go 直接转到 Caslte Black 而没有收集所有项目,它会以正确的消息完成,但我会收到以下错误:

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

Here is my code.这是我的代码。

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

When I change the argument from 0 to 1 and start the game over当我将参数从 0 更改为 1 并重新开始游戏时

city_item = cities[current_city][1]

I get the following error:我收到以下错误:

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

I am unsure which way I should go from here.我不确定我应该从这里拨打 go。

After the first condition in the loop, add a break or wrap the rest in an else .在循环中的第一个条件之后,添加一个break或将 rest 包装在else中。 You're trying to access cities["Castle Black"] , hence the KeyError .您正在尝试访问cities["Castle Black"] ,因此出现KeyError

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM