-2
좋아요, 파이썬에서 목록을 회전시키고 싶지만 특정 정수가 할당되어 있습니다. 내 질문은 어떻게 목록에서 특정 색인을 얻고 그 번호를 변수에 할당하는 것입니다. 제가 언급하고있는 요점은 "이것은 작동하지 않습니다"입니다. 내가 이것을 실행할 때 : TypeError : 'int'객체는 subscriptable이 아니다. 특정 인덱스를 변수에 할당하는 방법을 알 수 없습니다. 도와주세요.목록에서 정수를 선택하고 파이썬에서 할당하는 방법
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'는 목록이 아닙니다. – hop
왜 'room_number'의 값을 덮어 썼습니까? :'room_number = 0'. –
@ChristianDean 처음에는 숫자가 1-4가 아니길 원합니다. 루프에서 참이라는 것을 찾으려면 boolean false를 갖는 것과 같은 제로 (zero) 종류를 만들 필요가 있다고 생각했습니다. 필요하지 않니? – Andrew