簡體   English   中英

為什么沒有類型會給出 int 錯誤? 當刪除時表示索引必須是 int 而不是 str

[英]Why does none type give int error? When deleted says indicies must be int and not str

我正在制作一個基於房間的文字冒險游戲。 如果您運行代碼,它會在您進入房間邊界內時起作用(即從卧室 2 開始,反之亦然進入卧室 1),但是一旦超出邊界,就會出現錯誤:

>>> print(room_list[int(current_room)][0])
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

刪除int() ,出現不同的錯誤:

TypeError: list indices must be integers or slices, not str

如何修復它以保留None類型? 或者只是讓代碼工作。 下面是我的代碼。

def main():
    done = False
    room_list = []
    current_room = 0

    # Append rooms to different numbers
    room = ["\nYou are in bedroom 2.\nThere is a passage north and east.", "3", "1", None, None]  # Room name, N, E S, W
    room_list.append(room)

    room = ["\nYou are in at southern hall.\nThere is a passage  north and east.", "4", "2", None, "0"]
    room_list.append(room)

    room = ["\nYou are in the dining room.\nThere is a passage north and west.", "5", None, None, "1"]
    room_list.append(room)

    room = ["\nYou are in bedroom 1.\nThere is a passage east and south.", None, "4", "0", None]
    room_list.append(room)

    while not done:
        print(room_list[int(current_room)][0])
        answer = input('What direction? ')

        # Check for correct direction and check if exists
        if answer == "n":
            next_room = room_list[int(current_room)][1]
            current_room = next_room
            if next_room == None:
                print("You can't go that way. ")

        elif answer == "e":
            next_room = room_list[int(current_room)][2]
            current_room = next_room
            if next_room == None:
                print("You can't go that way. ")

        elif answer == "s":
            next_room = room_list[int(current_room)][3]
            current_room = next_room
            if next_room == None:
                print("You can't go that way. ")

        elif answer == "w":
            next_room = room_list[int(current_room)][4]
            current_room = next_room
            if next_room == None:
                print("You can't go that way. ")

        elif answer == "q":
            done = True

        else:
            print("Invalid destination, try again.")

main()

好吧,因為current_room (或next_room )是None ... 你應該在if語句之后移動current_room = next_room ,在else子句下

def main():
    done = False
    room_list = []
    current_room = 0

    # Append rooms to different numbers
    room = ["\nYou are in bedroom 2.\nThere is a passage north and east.", "3", "1", None, None]  # Room name, N, E S, W
    room_list.append(room)

    room = ["\nYou are in at southern hall.\nThere is a passage  north and east.", "4", "2", None, "0"]
    room_list.append(room)

    room = ["\nYou are in the dining room.\nThere is a passage north and west.", "5", None, None, "1"]
    room_list.append(room)

    room = ["\nYou are in bedroom 1.\nThere is a passage east and south.", None, "4", "0", None]
    room_list.append(room)

    while not done:
        print(room_list[int(current_room)][0])
        answer = input('What direction? ')

        # Check for correct direction and check if exists
        if answer == "n":
            next_room = room_list[int(current_room)][1]
            if next_room is None: # == None can be replaced with is None
                print("You can't go that way. ")
            else:
                current_room = next_room

        elif answer == "e":
            next_room = room_list[int(current_room)][2]
            if next_room is None:
                print("You can't go that way. ")
            else:
                current_room = next_room

        elif answer == "s":
            next_room = room_list[int(current_room)][3]
            if next_room is None:
                print("You can't go that way. ")
            else:
                current_room = next_room

        elif answer == "w":
            next_room = room_list[int(current_room)][4]
            if next_room is None:
                print("You can't go that way. ")
            else:
                current_room = next_room

        elif answer == "q":
            done = True

        else:
            print("Invalid destination, try again.")

main()

暫無
暫無

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

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