python编程训练题

根据校园地图,实现学校校园的文字版实景导航,从
起始位置为A1,通过选择不同的方向,前往下一个目的地,并且能通过交互实现回到起始点,即A1,从而实现对校园主要建筑的文字导航。

# define a dictionary to store the map information
map_dict = {
    "A1": ["Library", "North"],
    "A2": ["Cafeteria", "South"],
    "A3": ["Classroom Building", "East"],
    "A4": ["Student Center", "West"],
}

# define the current location
current_location = "A1"

while True:
    # print the current location and the available directions
    location_info = map_dict[current_location]
    print("You are at:", location_info[0])
    print("Available directions:", location_info[1])

    # get the user's choice of direction
    direction = input("Enter a direction (North/South/East/West) or 'q' to quit: ")

    # if the user chooses to quit, break the loop
    if direction == "q":
        break

    # if the direction is not available, print an error message
    if direction not in location_info[1]:
        print("Invalid direction. Please try again.")
        continue

    # if the direction is available, update the current location
    if direction == "North":
        current_location = "A1"
    elif direction == "South":
        current_location = "A2"
    elif direction == "East":
        current_location = "A3"
    elif direction == "West":
        current_location = "A4"