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 (2016, retired 2019) Dungeon Game Win or Lose

bayan elshiwy
bayan elshiwy
3,272 Points

i made the code exactly like kenneth wrote but there is 2 errors in dungeon game that i cannot fix what shall i do?

import random
import os

CELLS = [(0,0),(1,0),(2,0),(3,0),(4,0),
         (0,1),(1,1),(2,1),(3,1),(4,1),
         (0,2),(1,2),(2,2),(3,2),(4,2),
         (0,3),(1,3),(2,3),(3,3),(4,3),
         (0,4),(1,4),(2,4),(3,4),(4,4)]

def clear_screen():
    os.system('cls' if os.name == 'nt' else 'clear')

def get_locations():
    return random.sample(CELLS,3)

def move_player(player,move):
    x,y = player
    if move == "LEFT":
        x -= 1
    if move == "RIGHT":
        x += 1
    if move == "UP":
        y -= 1
    if move == "DOWN":
        y += 1
    return x,y


def get_moves(player):
    moves = ['LEFT','RIGHT','UP','DOWN']
    x,y = player
    if x == 0:
        moves.remove('LEFT')
    if x == 4:
        moves.remove('RIGHT')
    if y == 0:
        moves.remove('UP')
    if y == 4:
        moves.remove('DOWN')

    return moves
def draw_map():
    print(" "*5)
    tile = "|{}"
    for cell in CELLS:
        x,y = cell
        if x < 4:
            line_end = ""
            if cell == player:
                output = tile.format("X")
            else:
                output = tile.format(_)
        else:
            line_end = "/n"
            if cell == player:
                output = tile.format("X|")
            else:
                output = tile.format("_|")

        print(output, end=end_line)

def game_loop():            
    monster , door, player = get_locations()
    playing = True
    while playing:
        clear_screen()
        draw_map(player)
        valid_moves = get_moves(player)
        print("you are currently in room {}".format(player)) 
        print("you can move {}".format(",".join(valid_moves))) 
        print("Enter QUIT to quit")


        move = input(">").upper()
        move = move.upper()

        if move == "QUIT":
            print("see you next time!")
            break
        if move in valid_moves:
            player = move_player(player,move)
            if player == monster:
                print("oh no the monster got you better luck next time!!")

                playing = False
            if player == door:
                print("you escaped.. congratulations!")
                playing = False
    else:
        if input("play again? [y/n]").lower() != "n":
            game_loop()
        else:
            input("/n **walls are hard! dont run into them **/n")


clear_screen()
print("welcome to the dungeon game..")
input("press return to start")
clear_screen()
game_loop()

thats my code there is error that i couldnt fix error:

Traceback (most recent call last):                                                                                                                                                   
  File "dungeon_game.py", line 100, in <module>                                                                                                                                      
    game_loop()                                                                                                                                                                      
  File "dungeon_game.py", line 63, in game_loop                                                                                                                                      
    monster , door, player = get_locations()                                                                                                                                         
  File "dungeon_game.py", line 14, in get_locations                                                                                                                                  
    return random.sample(CELLS,3)                                                                                                                                                    
AttributeError: module 'random' has no attribute 'sample'

3 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Bayan;

When running the above code (which I adjusted the format on for prettier output here in the forum), I didn't get the error you posted.

However, I did come across several other errors based on the above code. Python typically provides pretty good trace back information stating which line has a problem which is helpful for debugging (most of the time).

1) Your draw_map() method currently isn't taking any arguments. When you define it on line 42 it should be def draw_map(player): 2) The underscore on line 52 should be in quotes, format("_") 3) The escaped return line character on line 54 should have a back-slash instead of a forward slash, line_end = "\n" 4) On line 60 you are setting end=end_line but you are defining that on line 54 as line_end.

With those changes, I was able to get the above code to run as expected.

Post back if you are still stuck.

Ken

bayan elshiwy
bayan elshiwy
3,272 Points

thanks in advance , but there are same errors Traceback (most recent call last):
File "dungeon_game.py", line 100, in <module>
game_loop()
File "dungeon_game.py", line 63, in game_loop
monster , door, player = get_locations()
File "dungeon_game.py", line 14, in get_locations
return random.sample(CELLS,3)
AttributeError: module 'random' has no attribute 'sample'

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Which version of Python are you running?

What development environment are you using (Workspaces, IDE, text editor, etc.)?

What is the name of the files in your project? Do you have another file named random in there? If so it is generally a best practice to not name your project files after Python libraries.

Please post back with additional information.

Thanks,
Ken