2017-12-17 9 views
-2

いいえ、私は回りたいと思うリストをPythonで持っていますが、それに特定の整数が割り当てられています。私の質問は、リストから特定のインデックスを取得し、その番号を変数に割り当てる方法です。私が言及している点は、「これは働かない」ということです。私がこれを実行すると、次のようになります:TypeError: 'int'オブジェクトは添え字ではありません。その特定のインデックスを変数に代入する方法はわかりません。助けてください。リストから整数を選択してPythonで割り当てる方法

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 
+0

'room_number'はリストではありません – hop

+1

なぜあなたは' room_number'の値を上書きしましたか?: 'room_number = 0'。 –

+0

@ChristianDean私は最初に1-4にならないようにしたい。私はループで真であることを見たい場合は、真偽値を持つようなゼロの種類にする必要があると思った。私はそれを必要としませんか? – Andrew

答えて

1

あなたが最初に持っている:

room_number = [1, 2, 3, 4] 

を後から設定した:あなたは、エラーメッセージを取得している理由

room_number = 0 

第二の割り当てがあります。

関連する問題