How do you make a simple 3x3 maze on python using lists?

独自空忆成欢 提交于 2019-12-02 14:21:08

I started doing this as a fun thing to try. (And this below might not be an answer but anyway... posting it)

But let me tell you something: The guide is very "low" quality (Yes I might be too harsh but hey! just an opinion). Even thought this is beginner programming I don't see a reason not to introduce dictionaries and other common tools.

I stopped at point 10 or 11 and continued with what I had so far. Basically the goal is to move to the kitchen according to the figure in the site you gave me. If you are interested to understand more or have any questions I can gladly answer them.

Good luck :)

import sys
room_list = [] 

#2,3,4
room_list.append(["Bedroom 2, You can go north and east.",3,1,None,None])
room_list.append(["South Hall, ...",4,2,None,0])
room_list.append(["Dining Room, ...",5,None,None,1])
room_list.append(["Bedroom 1, ...",None,4,0,None])
room_list.append(["North Hall, ...",6,5,1,3])
room_list.append(["Kitchen, ...",None,None,2,4])
room_list.append(["Balcony, ...",None,None,4,None])

#5
current_room = 0

#6
#print(room_list)

#7
#print(room_list[0])

#8
#print(room_list[current_room])

#9
#print(room_list[current_room][0])

#10
done = False


#10++ Here I started writing my own code... :)

directions = {"North":1,"East":2,"South":3,"West":4}

#11
while not done:

    print("\n"+room_list[current_room][0])
    q = "0"
    valid_choice = False

    while not valid_choice:

        while not directions.get(q.title()):
            if q != "0":
                print("Try again...")
            q = raw_input("Where do you want to go? (North,East,South or West) Or Quit (Q)")
            if q.title() == "Q":
                print("You pressed Q. Exit")
                sys.exit()

        next_move = directions.get(q.title()) 
        next_room = room_list[current_room][next_move]

        if next_room:
            valid_choice = True
            current_room = next_room                
        else:
            print("You can't go that way")
            q = "0" # reset question

    if current_room == 5:
        print("\nYou are in: "+room_list[5][0])
        done = True

Q: Hi, thanks for this! If you don't mind me asking, what do the directions = {"North":1,"East":2,"South":3,"West":4} and the directions.get(q.title()) do?

directions = {"North":1,"East":2,"South":3,"West":4} in this case is a dictionary. It contains keys (North,East,South,West) and their corresponding values (1,2,3,4). So when you type directions["North"] you actually get the value 1.

There is however another way to get the values and that is by using the directions.get() function. When you do and the key does not exist None is returned. E.g. directions.get("monkey") = None but directions.get("East") = 2. This is quite useful when we use a while not loop. Until the user inputs something valid the q will be None and the question repeated.

Finally q is the string that was returned from the question. And str.title() function returns the string with the first letter capital. E.g. "hellOOO".title() = "Hellooo" and that is why you can type in "easT" and still get "East" which is the value you need to make the directions.get("East") work.

Q: one more question (sorry i'm quite new at this), if i wanted to add a quit function, where would i insert it? i tried placing it in lines before else statements but i get errors.

I added:

import sys

And

if q.title() == "Q":
    print("You pressed Q. Exit")
    sys.exit()
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!