Python Text RPG-难以处理while循环和if语句

时间:2019-03-10 07:42:37

标签: python

room_list确实很大,问题出在函数中,因此我没有在代码中包含该部分。列表中的每个条目基本上是:

[room_description, # of room North, East, South, West]

问题在于,每当我测试while循环并且如果我输入的语句处理无效输入时,程序就会陷入while循环中,而我不知道如何解决它。

def room_movement():
    current_room = 0
    done = False
    print("")

while done is False:
    print(room_list[current_room][0])
    print("")
    direction = input("Which direction do you want to go?\n\n")
    direction = direction.lower()

    if direction == "north":
        next_room = room_list[current_room][1]
        if next_room is None:
            while next_room is None:
                print("When you try to move in that direction you are blocked by an invisible wall. Try again")
                direction = input("\n")
                if direction == "east":
                    next_room = room_list[current_room][2]
                if direction == "south":
                    next_room = room_list[current_room][3]
                if direction == "west":
                    next_room = room_list[current_room][4]
                else:
                    direction = input("That wasn't a valid direction... Try again.\n")
        else:
            current_room = next_room
            print(current_room)
        current_room = next_room
        print(current_room)

    elif direction == "east":
        next_room = room_list[current_room][2]
        if next_room is None:
            while next_room is None:
                print("When you try to move in that direction you are blocked by an invisible wall. Try again")
                direction = input("\n")
                if direction == "north":
                    next_room = room_list[current_room][1]
                if direction == "west":
                    next_room= room_list[current_room][4]
                if direction == "south":
                    next_room = room_list[current_room][3]
                else:
                    direction = input("That wasn't a valid direction... Try again.\n")
        else:
            current_room = next_room
            print(current_room)
        current_room = next_room
        print(current_room)

    elif direction == "south":
        next_room = room_list[current_room][3]
        if next_room is None:
            while next_room is None:
                print("When you try to move in that direction you are blocked by an invisible wall. Try again")
                direction = input("\n")
                if direction == "north":
                    next_room = room_list[current_room][1]
                if direction == "east":
                    next_room = room_list[current_room][2]
                if direction == "west":
                    next_room = room_list[current_room][4]
                else:
                    direction = input("That wasn't a valid direction... Try again.\n")
        else:
            current_room = next_room
            print(current_room)
        current_room = next_room
        print(current_room)

    elif direction == "west":
        next_room = room_list[current_room][4]
        if next_room is None:
            while next_room is None:
                print("When you try to move in that direction you are blocked by an invisible wall. Try again")
                direction = input("\n")
                if direction == "north":
                    next_room = room_list[current_room][1]
                if direction == "east":
                    next_room = room_list[current_room][2]
                if direction == "south":
                    next_room = room_list[current_room][3]
                else:
                    direction = input("That wasn't a valid direction... Try again.\n")
        else:
            current_room = next_room
            print(current_room)
        current_room = next_room
        print(current_room)
    else:
        while direction != "north" or "south" or "west" or "east":
            print("That command is beyond the system's intelligence. Try again")
            direction = input("\n")

room_movement()

0 个答案:

没有答案