如何从列表中选择整数并在Python中分配它
好的,我在Python中有一个列表,我想循环使用,但也给它指定了一个整数。我的问题是如何从列表中获取某个索引,并将该编号分配给变量。我指的是这不起作用的原因。当我运行此命令时,我得到:TypeError:“int”对象不可下标。我想不出一种方法来将某个索引分配给一个变量。请帮忙如何从列表中选择整数并在Python中分配它,python,list,indexing,Python,List,Indexing,好的,我在Python中有一个列表,我想循环使用,但也给它指定了一个整数。我的问题是如何从列表中获取某个索引,并将该编号分配给变量。我指的是这不起作用的原因。当我运行此命令时,我得到:TypeError:“int”对象不可下标。我想不出一种方法来将某个索引分配给一个变量。请帮忙 entryway1 = ['another locked door', 'a hallway-1', 'a hallway-2', 'a room'] came_from = ['right', 'left', 'for
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
第二个赋值是您收到错误消息的原因。room\u number不是列表为什么覆盖room\u number的值?:room\u number=0。@ChristianDean我希望最初的数字不是1-4。我想如果我想在循环中找到一个为真的布尔值,我需要把它设为零。我不需要它吗?