Reputation: 31
I know that if I want to randomly generate a number I do something like this
import random
run = -1
for x in range(10):
rand = random.randint(1, 30)
but how can I get a random generation of yes or no instead of numbers?
Upvotes: 2
Views: 9254
Reputation: 21643
You can do it very directly with choice
from the standard module random
.
>>> from random import choice
>>> answer = choice(['yes', 'no'])
>>> answer
'yes'
Upvotes: 8
Reputation: 1548
A simple coin toss would be something like this
def coin_toss(p=.5):
return 'yes' if random.random() < p else 'no'
Upvotes: 4
Reputation: 464
The best way would be to make a list then select one of the attributes like so:
import random
l1 = ["yes", "no"]
for x in range(5):
rand = random.randint(0, 1)
print(l1[rand])
Hope I helped!
-Zeus
Upvotes: 1