Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (Retired) Dungeon Game Random Choices

Python Collections Course

I'm stuck again at another code challenge it seems that my problem is I can't think of ways how to do these challenges and I often need to look up the answer for help on the forums. In Python basics I understood everything and could do it no problem but for collections I find myself stuck at challenges for hours before looking up the answer. I'm not sure what to do I know what these things do it's just I can't think of a way how to do them.

For this challenge I understand I must make an iterable and set an integer then pick a random choice from that iterable 'integer' amount of times but I'm stuck. I don't know how to do that. And It's frustrating me to no end

Should I just leave Python for now and try to do another course that will maybe help me maybe understand programming logic and help develop these algorithms? What can I do to get a better grasp on what's going on?

choices.py

2 Answers

Seth Reece
Seth Reece
32,867 Points

Hi Darrian,

Don't get frustrated. Kenneth Love makes his challenges very challenging. He generally asks you to do something that wasn't completely covered. I've found it best to go to the documentation, and play around with my code locally, (or in workspaces). I get better error messages than "bummer" this way. For this challenge, you want to import random, create your function, make an empty list, and a counter variable, then do a while loop and increment the counter until it reaches the integer passed in when the function is called. Append a random item from the iterable that is passed in to the empty list you made, then return the list. e.g.

import random
def nchoices(my_iter, my_int):
  i = 0
  my_list = []
  while i < my_int:
    my_list.append(random.choice(my_iter))
    i += 1
  return my_list

When working locally, I also made a variable calling the function and print it to see what's going on. e.g.

import random
def nchoices(my_iter, my_int):
  i = 0
  my_list = []
  while i < my_int:
    my_list.append(random.choice(my_iter))
    i += 1
  return my_list

new_list = nchoices([1, 2, 3, 4,5, 6], 4)
print(new_list)

awesome thank you. I do use workspaces to try code for better error messages, but I've noticed ill go through trial and error trying to work on a challenge forever and when I see how simple the answer is (like yours) I realize i make things harder than they should be

ill continue with the course getting stuck on these challenges and hopefully it will all come to me in time