Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (Retired) Dungeon Game Building the Game: Part 2

Aya Yousef
Aya Yousef
9,658 Points

Dungeon-game error

Hello: I'm having an error which appears when I try to start my game Here's the error:

'''

Traceback (most recent call last):
File "dungen-game.py", line 84, in <module>
moves = get_moves(player)
File "dungen-game.py", line 55, in get_moves
moves.remove('up')
ValueError: list.remove(x): x not in list

'''

And here's is my code from line:83 to the end

'''

while True:

moves = get_moves(player)
print("Your current location is {}.".format(player))
draw_map(player)
print("You can move {}.".format(moves))
print("Enter 'QUIT' to quit game.")  
move = input("> ")
move = move.upper()
if move == "QUIT":
    break
if move in moves:
    player = move_player(player, move)
else:
    print("** Walls are hard. Stop walking into them! **")
    continue
if player == door:
    print("You escaped!")
    break
elif player == monster:
    print("You were eaten by the grue!")
    break       

'''

my moves function around line 55:

'''

def get_moves(player):

moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']
if player[0] == 0:
    moves.remove('up')
if player[0] == 2:
    moves.remove('DOWN')
if player[1] == 0:
    moves.remove('LEFT')
if player[1] == 2:
    moves.remove('RIGHT')
return moves

'''

1 Answer

Josh Keenan
Josh Keenan
20,315 Points

Try replacing each move removal function with something else, like this:

del moves[index of move]

# like this
moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']
if player[0] == 0:
    del moves[2]
if player[0] == 2:
    del moves[3]
if player[1] == 0:
    del moves[0]
if player[1] == 2:
    del moves[1]
return moves
Aya Yousef
Aya Yousef
9,658 Points

It worked, thank you.. but what's wrong in my code? why did we replace remove fun with del keyword.. aren't they supposed to be the same?

Josh Keenan
Josh Keenan
20,315 Points

I am not sure but perhaps using this could affect it.

moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']
if player[0] == 0:
    moves.remove('up')  # <-- Instead use 'UP'
Aya Yousef
Aya Yousef
9,658 Points

Yeah right.. I forgot to caps 'up'. Thank you Josh

Josh Keenan
Josh Keenan
20,315 Points

Not sure if that is the only problem but you do need to be that specific.. Happy to help :)