如何从列表中选择整数并在Python中分配



好吧,我在python中有一个我想旋转的列表,但也分配了一定的整数。我的问题是如何从列表中获取某个索引并将该数字分配给变量。我指的是"这不起作用"。当我运行此操作时,我会得到:typeError:'int'对象无法订阅。我无法找到一种将某些索引分配给变量的方法。请帮助。

entryway1 = ['another locked door', 'a hallway-1', 'a hallway-2', 'a room']
came_from = ['right', 'left', 'forward', 'backward']
room_number = [1, 2, 3, 4]

def rotate_number(room_list, n): #This function rotates/shifts the room number values 
    room_list = (room_list[-n:] + room_list[:-n])
    return room_list

def rotate(list_1, n): #This function rotates/shifts the room sides values
    list_1 = (list_1[-n:] + list_1[:-n])
    return list_1

def room_1():  # This is one of four "rooms" surrounding the middle room
    global from_direction
    print("This is the locked door")
    from_direction = 'locked door'

def room_2():  # This is one of four "rooms" surrounding the middle room
    global from_direction
    print("This is hallway 1")
    from_direction = 'hallway 1'

def room_3():  # This is one of four "rooms" surrounding the middle room
    global from_direction
    print("This is hallway 2")
    from_direction = 'hallway 2'

def room_4():  # This is one of four "rooms" surrounding the middle room
    global from_direction
    print("This is a room")
    from_direction = 'room'

def look2():  # This is the middle room named the entryway.
    print("I must be in an entryway.")
    global entryway1
    global room_number
    global from_direction
    if from_direction == 'locked door':  # The four ifs check what direction you came from and sets the room up to that certain perspective through lists.
        entryway1 = rotate(entryway1, 3)
        room_number = rotate_number(room_number, 3)
    if from_direction == 'hallway 1':
        entryway1 = rotate(entryway1, 2)
        room_number = rotate_number(room_number, 2)
    if from_direction == 'hallway 2':
        entryway1 = rotate(entryway1, 1)
        room_number = rotate_number(room_number, 1)
    if from_direction == 'room':
        entryway1 = rotate(entryway1, 0)
        room_number = rotate_number(room_number, 0)
        print(entryway1)

这是我似乎无法正常工作的部分。从列表中分配int的东西。

    room_number = 0
    movearea = True
    print("Where to go...")
    while movearea:    # This allows you to look around the room freely and sets a value according to what way you last looked at
        direction = input().lower()
        if direction == 'Look Right'.lower():
            print("*There is", entryway1[0], "there.*")  
            to_next_room = room_number[0]    # THIS DOESN'T WORK 
        if direction == 'Look Forward'.lower():
            print("*There is", entryway1[1], "there.*")  
            to_next_room = room_number[1]    # THIS DOESN'T WORK 
        if direction == 'Look Left'.lower():
            print("*There is", entryway1[2], "there.*") 
            to_next_room = room_number[2]    # THIS DOESN'T WORK 
        if direction == 'Look Back'.lower():
            print("*There is", entryway1[3], "there.*")  
            to_next_room = room_number[3]    # THIS DOESN'T WORK 
        if to_next_room == 0 and direction == 'open door': #the to_next_room checks what value was last assigned from looking at a certain direction. It also checks if you put in the right input to go to the next room.
            print('escape_game() was originally right')
            room_1()
        if to_next_room == 1 and direction == 'go there':
            print('forward1() was originally forward')
            room_2()
        if to_next_room == 2 and direction == 'go there':
            print('left1() was originally left')
            room_3()
        if to_next_room == 3 and direction == 'go there':
            print('emptyroom1() was originally back')
            room_4()
        else:
            pass

print("You are in a a locked door")  # This is a basic starting point for the game that this chunk of code is from.
room_3()   # This changes in the whole piece of code, but for testing I just 
type rooms 1-4 in here
next_room = input("would you like to move on?")
if next_room == 'yes':
    look2()
if next_room == 'no':
    print("ok bye")
else:
    pass

您最初有:

room_number = [1, 2, 3, 4]

然后您设置:

room_number = 0

第二个分配是为什么您会收到错误消息。

最新更新