How do I perform a random event in Python by picking a random variable?

前端 未结 2 407
醉梦人生
醉梦人生 2021-01-14 13:40

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?

2条回答
  •  借酒劲吻你
    2021-01-14 13:57

    import random
    print random.choice([dog, cat])
    

    It's that simple. choice() takes a sequence and returns a random selection from it.

提交回复
热议问题