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

Rocco Soucie
Rocco Soucie
1,018 Points

How do I target a randomly selected item from a list and pop it?(Python)

I am semi new to python and I decided to create a random number generator to help pick challenges for my video game. I have had lots of success so far in picking a random number from the list, giving it a difficulty fro 1-20, and have it loop through as many characters as the user wants. But I don't want the randomly selected character to be able to be selected again in the next loop through. How to I target the randomly selected character that the code has printed so it can be popped from the list? Also I don't know how to post a picture of my code from the treehouse work space. THX!

1 Answer

Steven Parker
Steven Parker
230,248 Points

The "pop" method takes an optional argument to specify which item to be removed instead of the last. Just supply the index number of the item to remove.

Posting screen images is usually not very useful, but the workspace has a special feature just for sharing. You can make a snapshot of your workspace and post the link to it here. The snapshot contains all the code and other resources of your project.

Rocco Soucie
Rocco Soucie
1,018 Points

Okay, but I can't use the index number because it was randomly generated, and I don't know which item was found. I want to be able to target the specific character that was randomly found and pop it, so when it loops again, I won't get any of the same characters.

Steven Parker
Steven Parker
230,248 Points

Didn't the "random" method involve creating an index to pick from? I would think the easiest technique would be to use "pop" to select the item to begin with. And if not, you can use the "index" method to find the index of a particular item.

If you need additional help, please include that snapshot link.

Rocco Soucie
Rocco Soucie
1,018 Points

Ok here is the the code:

https://w.trhou.se/ub8c1mywoc

I used random.choice to select what character and difficulty randomly from the lists. But I want to know how I can pop the character that was randomly chosen.

Steven Parker
Steven Parker
230,248 Points

Your workspace has a lot of files, I'm guessing that "custom_night.py" is the one we're discussing.

So if you pass the random choice to the "index" function as I suggested, you can use it to pop that same iitem from the list:

    print(animatronics.pop(animatronics.index(random.choice(animatronics)))
          + "  " + random.choice(difficulty))

It's slightly simpler if you use my first suggestion of picking a random index number:

    print(animatronics.pop(random.randrange(len(animatronics)))
          + "  " + random.choice(difficulty))