1

i am very new to python and am trying to learn the language better. i am trying to create something very simple, i am playing around with if statements, i am trying to choose a random option and based on what is picked have a different response. what i have looks like this...

species = "cat" or "dog"
if species == "cat":
    print("yep, it's a cat")
else:
    print("nope, it's a dog")

but the outcome is always cat. how do i make it randomly choose?

  • The `or` operator in your first line stops evaluating as soon as one of the strings is evaluated as True. A string that contains data always evaluates as true. If you wanted "dog" from that first line, the first value would need to be false. – Sam Morgan Jul 20 '20 at 02:14

1 Answers1

8

Use random module and choice method on the list

import random

species_list = ["cat", "dog"]
species = random.choice(species_list)
if species == "cat":
    print("yep, it's a cat")
else:
    print("nope, it's a dog")
bigbounty
  • 16,526
  • 5
  • 37
  • 65