4
\$\begingroup\$

I'm sure there are better ways to code this. I spent a lot of time re-working the code to get it as clean as possible. I would appreciate any input on what I could change to have the program run more efficiently and look cleaner.

outer = False
while outer == False:
    room_list = []
    room = ["This place stinks, better get moving. Door to the N or E", 1, 8, None, None]
    room_list.append(room)
    room = ["You are in a safe room. There are doors to the N,E,S and W", 2, 9, 0, 4]
    room_list.append(room)
    room = ["This room is filled with garbage. There is a door to the S", None, None, 1, None]
    room_list.append(room)
    room = ["You encounter goblins and die!"]
    room_list.append(room)
    room = ["You encounter orcs and kill them all! There is a door to the N,E and S", 3, 1, 5, None]
    room_list.append(room)
    room = ["You encounter rats, easy fight. Theres a door to the N", 4, None, 7, None]
    room_list.append(room)
    room = ["You found a companion! Theres a door to the S", None, None, 9, None]
    room_list.append(room)
    room = ["You found the treasure! Congratulations!"]
    room_list.append(room)
    room = ["Ouch, you were hit by a fireball! There's a door to the W", None, None, None, 0]
    room_list.append(room)
    room = ["You found a treasure map! It says 'Rats dwell, south it lies'. Theres a door to the N and W",6,None,None,1]
    room_list.append(room)
    current_room = 0
    print (room_list[current_room][0])
    inner = False
    new_room = 0
    while inner == False:
        user_choice = input ("What would you like to do? ")
        print ()
        if user_choice in('north','n','North','N'):
            new_room = room_list[current_room][1]
        elif user_choice in('east','e','East','E'):
            new_room = room_list[current_room][2]
        elif user_choice in('south','s','South','S'):
            new_room = room_list[current_room][3]
        elif user_choice in('west','w','West','W'):
            new_room = room_list[current_room][4]
        else:
            print ("I don't know what", user_choice, "means")

        if new_room == None:
            print ("You can't go that way.")
        else:
            current_room = new_room
            print (room_list[new_room][0])


        if current_room == 3:
            play_again = input("Would you like to play again? ")
            if play_again in("y","Y","Yes","yes"):
                print()
                break
                    else:
                        outer = True
                        break
        if current_room == 7:
            outer = True
            break
\$\endgroup\$

1 Answer 1

5
\$\begingroup\$

Actually, the general idea of a list of positions is really neat. What is not so neat is that you create the list on each iteration. That is unnecessary. You should just create the list before the loop, and leave it there. You also don't need those long lines of .append()s. Just make the whole list like this:

room_list = [
    ["This place stinks, better get moving. Door to the N or E", 1, 8, None, None],
    ["You are in a safe room. There are doors to the N,E,S and W", 2, 9, 0, 4],
    ["This room is filled with garbage. There is a door to the S", None, None, 1, None],
    ["You encounter goblins and die!"],
    ["You encounter orcs and kill them all! There is a door to the N,E and S", 3, 1, 5, None],
    ["You encounter rats, easy fight. Theres a door to the N", 4, None, 7, None],
    ["You found a companion! Theres a door to the S", None, None, 9, None]
    ["You found the treasure! Congratulations!"],
    ["Ouch, you were hit by a fireball! There's a door to the W", None, None, None, 0],
    ["You found a treasure map! It says 'Rats dwell, south it lies'. Theres a door to the N and W",6,None,None,1],
]

Another thing I would suggest is to have somebody else proofread your sentences. You have a couple cases of Theres when it should be There's. You also in many places leave out the Oxford comma. I will grant that the Oxford comma is controversial, and I will probably get a comment or two in strong disagreement with the use of it.


When you use if ...:, Python sees if ... is a Truthy value, and if it is, executes the suite. Therefore, if ... == True: is redundant (and sometimes comes out with unexpected results; see this SO answer). Consequently, if ... == False: could be simply written as if not ...:. With all of these examples, it works the same way with a while loop. It is in fact your while loops that bring it up.


You have some strange uses of whitespace. In Python 3, print is a function. Therefore, you should use print(...), not print (...). With the space, it looks like you are using it like a statement and whatever is in the parentheses might be a tuple to be printed instead of a list of arguments that will each be printed. Then again, you use if ... in(...). Well, in is not a function. Therefore, you should add the space to make that more clear that the parentheses are there for the tuple, not as a way of passing arguments.


Your if and elses could be simplified:

rooms = {'n': 1, 'e': 2, 's': 3, 'w': 4}
user_choice = user_choice.lower()
if len(user_choice) != 1 and user_choice not in ('north', 'south', 'east', 'west'):
    print("I don't know what '{}' means".format(user_choice))
    continue
new_room = room_list[current_room][rooms[user_choice[0]]]

According to PEP 8, "Comparisons to singletons like None should always be done with is or is not, never the equality operators."


I like that you check if the user wants to play again, but what if the user types Certainly!? I can't expect you to make your program expect things like that, but it should at least show that it didn't expect it. As it is, it exits. It just assumes that anything not y or yes is no. You should use a while loop that keeps asking for input until the user types a definite y, yes, n, or no. Also, your case-insensitivity is nice, but it could be made simpler. If you just say if plag_again.lower() in ('y', 'yes'), your program will now accept even yEs and YeS.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.