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 trialJothum Chitewe
7,833 Pointssample.py
help needed
import random
import os
import sys
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():
if os.name == 'nt':
os.system('cls')
else: os.system('clear')
def get_locations():
return random.sample(cells,3)
def move_player(player, move):
#ascertain current player location
#if move == LEFT, x-1
#if move == RIGHT, x+1
#if move == UP, y-1
#if move == DOWN, y+1
return player
def get_move(player):
move = ["LEFT", "RIGHT", "UP", "DOWN"]
x, y = player
if x == 0:
move.remove("LEFT")
if x == 4:
move.remove("RIGHT")
if y == 0:
move.remove("UP")
if y == 4:
move.remove("DOWN")
return move
monster, door, player = get_locations()
while True:
print("Welcome to the DUNGEON !")
print("You are currently in room {}".format(player))
print("You can move {}".format(", ".join(get_move(player))))
print("Enter QUIT to quit")
move = input("> ")
move = move.upper()
if move == 'QUIT':
print("I guess you were afraid of the DUNGEON!")
break
2 Answers
Dane Parchment
Treehouse Moderator 11,077 PointsWhy do you have all of that extra code? Read the instructions of the challenge:
I haven't shown you how to use this function yet but I'm sure you can use it. In the random
library, there's a function named sample
that takes two arguments: an iterable to sample from, and an integer of how many unique samples to return.
Finish the get_locations
function so that it returns 3 unique values from the cells
argument.
So simply do what it is asking you to do, in fact you already did it!
def get_locations():
return random.sample(cells,3)
There done! Just remove all of the extra code that you have written, it is not necessary and will cause the submission to fail!
Christopher Muzavazi
2,303 Pointsthis worked for me
def get_locations(cells):
return random.sample(cells,3)
Christopher Carucci
1,971 PointsChristopher Carucci
1,971 PointsCopy and pasted all of the code, I'm on the same thing and I don't get all of that code