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 Random Choices

Andrew K
Andrew K
13,774 Points

Why doesn't my choices.py code work?

Hi there,

The challenge asks me to create a function that returns a list of random items from an iterable. The function is supposed to take an iterable and an integer.

I figured out how to do this with:

import random

def nchoices(iterable, integer):
  list_of_items = []
  for number in range(integer):
    list_of_items.append(random.choice(iterable))
  return list_of_items

but initially, I'd tried:

import random

def nchoices(iterable, integer):
  list_of_items = []
  count = 0
  if count < integer:
    list_of_items.append(random.choice(iterable))
    count += 1
  return list_of_items

Why didn't the one with the if loop pass? (The message said got 1 instead of 5 items, but I couldn't figure out why). Thanks!

4 Answers

Ricky Catron
Ricky Catron
13,023 Points

I believe what you are looking for when you say "if loop" is a while loop: A while loop will keep going while its conditional evaluates to True.

Example:

import random

def nchoices(iterable, integer):
  list_of_items = []
  count = 0
  while count < integer:
    list_of_items.append(random.choice(iterable))
    count += 1
  return list_of_items

Hi Andrew,

The if block isn't a loop. The code inside the if block will execute 1 time if the condition is true and 0 times if the condition is false.

So your second example will always retrieve 1 or 0 items depending on the integer passed in.

Your working example is a loop which can run as many times as needed.

Yes what said above is correct, even though you have set the counter there, it won't help iterate the value of count as long as it is less than integer. So basically, from what I understand, what your code does

  if count < integer:
    list_of_items.append(random.choice(iterable))
    count += 1
  return list_of_items

is.... if count is less than the value of integer (I am not sure what its value it but I will assume, larger than 0) append a random item in the list of items, increase the counter number and return the list of item which is 1 random choice. So it basically returns only 1 item because that "if" runs only 1 time oppose to the "for" loop that runs as long as the value of number is not larger than integer. I hope my explanation makes sense.

Andrew K
Andrew K
13,774 Points

Thanks to all three of you--I had been getting some things jumbled up in the ol' noggin, and you've collectively put them back in order. Cheers!

Ricky Catron
Ricky Catron
13,023 Points

No worries man, is this your first programming language?

Ricky Catron
Ricky Catron
13,023 Points

Python is a great first language and once you understand these concepts most of them will transfer over to a new language and you will be able to dodge this entire stage completly! Its gonna be hard in the early stages, the best advice I can give you is code everything! The more you write the easier it will become and then you can really start to get deep. Goodluck!