从列表中指定要打印的新值

时间:2015-06-01 15:02:25

标签: python python-3.x

我尝试做的是当我按下' n'在while循环中,当我输入的下一个房间时,将打印一个新值。

#List of the rooms in my ship
room_list = []

#0
room = ['---text--- North.', 1, None, None, None]
room_list.append(room)

#1
room = ['---text--- East.', None, None, 3, None]
room_list.append(room)

#2
room = ['---text--- North, South, East and West.', 1, 2, 3, 4]
room_list.append(room)

#3
room = ['---text--- West.', None, None, None, 4]
room_list.append(room)

#4
room = ['---text--- East.', None, None, 3, None]
room_list.append(room)

#5
room = ['---text--- North, South, East and West.', 1, 2, 3, 4]
room_list.append(room)

#6
room = ['---text--- West.', None, None, None, 4]
room_list.append(room)

#7
room = ['---text--- South', None, 2, None, None]
room_list.append(room)

#Looping and room variable
current_room = 0
done = False

#Loop
while done == False:
    print(room_list[current_room][0])
    choice = input('\nWhere do you want to go? ')
    if choice == 'n':                           #Problem seems to be here
        next_room = room_list[current_room][2]
    if choice == None:
        print('You can not go that way!')

我在问题所在地发表评论。在我选择' n'之后,我不知道如何分配价值。打印列表中的2号房间。当我运行它,它的工作原理。但它只是循环,它不会改变从列表中打印出来的值。

3 个答案:

答案 0 :(得分:1)

您需要检查room_list[current_room][2]是否为表示有效房间的整数,或者None是否为room_list[current_room][2],并告诉用户他们不能这样做。如果while done == False: print(room_list[current_room][0]) choice = input('\nWhere do you want to go? ') if choice == 'n': if room_list[current_room][1] is not None: current_room = room_list[current_room][1] else: print('You can not go that way!') 是整数,请更新current_room:

[description, 1, None, None, None]

我改变了" north"的索引。是1,因为0号房间有directions = {'n':1, 's':2, 'e':3, 'w':4} while done == False: print(room_list[current_room][0]) choice = input('\nWhere do you want to go? ') if choice in directions: if room_list[current_room][directions[choice]] is not None: current_room = room_list[current_room][directions[choice]] else: print('You can not go that way!') 而且我认为你希望玩家能够去某个地方!

此外,为了节省大量重复代码,您可以制作方向词典:

redirect_to users_url

答案 1 :(得分:1)

请试试这个:

function check_view(objid)
            {
                var childid = $(objid).attr('ID');
                var childId1 = childid.substr(childid.length - 2);



                if ($(objid).is(':checked')) {

                        $(childId1).attr('checked', true);
                        $("[id$=chpMain_rptDocAccessroles_cb_viewaccess" + childId1 + "]").attr('checked', true);

                }

            }

答案 2 :(得分:0)

您必须更新current_room。另请注意,input(...)的结果不能为None,它始终是一个字符串:

if choice == 'n':
    next_room = room_list[current_room][2]
    if next_room == None:
        print('You can not go that way!')
    else:
        current_room = next_room