簡體   English   中英

我相信我有一個 Function 爭論問題

[英]I believe I have a Function Argument Issue

在大多數情況下,我的所有代碼似乎都運行良好。 該代碼是一個基於文本的游戲。 當我收集所有項目時,它可以正常運行而不會出錯。 但是,如果我 go 直接轉到 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 + "!\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 並重新開始游戲時

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

我不確定我應該從這里撥打 go。

在循環中的第一個條件之后,添加一個break或將 rest 包裝在else中。 您正在嘗試訪問cities["Castle Black"] ,因此出現KeyError

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM