Let's say I have to variables, dog and cat. Dog = 5, and cat = 3. How would I tell Python to pick one of these variables by random and print it to the screen?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
It's that simple.
choice()
takes a sequence and returns a random selection from it.You can put all the variables you want to choose from in a list and use the random module to pick one for you.
The sample method takes two arguments: the population you want to choose from, and the number of samples you want (in this case you only want one variable chosen).