I am trying to have a situation in which when we toss two coins at the same, if we get heads on both coins we win and if we get tails on both coins we lose.
I have been able to generate the results of tossing one of the coins separately using:
def coin_one():
one = [random.randint(1, 100) for x in range(100)]
for x in one:
if x <= 50:
print('Heads')
else:
print('Tails')
and also for the second coin using basically the same method:
def coin_two():
two = [random.randint(1, 100) for x in range(100)]
for x in two:
if x <= 50:
print('Heads')
else:
print('Tails')
I am trying to add a condition that will print('win') if we have 'heads' in both coin_one and coin_two when the two coins are tossed at the same time. How do I do this?
Why not combine both computations under one method, and do the checks in one pass? Since you're randomizing a coin flip; a binary value of 0/1 is enough to represent this probability accurately ( with the added bonus of using their implicit bool
values to do the equality checks ).
def coin_toss():
first_coin_tosses = [random.randint(0, 1) for x in range(100)]
second_coin_tosses = [random.randint(0, 1) for x in range(100)]
for first_coin_toss, second_coin_toss in zip(first_coin_tosses, second_coin_tosses):
if first_coin_toss and second_coin_toss: # Both 1's
# We win.
elif not first_coin_toss and not second_coin_toss: # Both 0's
# We lose.
else:
# if we get a 1 on one coin and and 0 on the other
# or vice versa then we neither win nor lose (we try again).
Write two functions that will return random value from 1-100. Then write below condition like If firstnum >50 and secondnun>50: Print head Else: Print tail
You can achieve your result in one of the following two ways.
First method:
This is straight forward solution. Store all the coin one and coin two results in a list and return it to the calling method and check for the equality.
Second method:
You can return (actually yielding) back for each coin 1 and 2 value and check it in the calling method.
import random
def coin_one():
one = [random.randint(1, 100) for x in range(100)]
for x in one:
if x <= 50:
print('Heads')
#Use yield to return value and also continue method execution
yield 'Heads' #Add this line
else:
print('Tails')
yield 'Tails' #Add this line
def coin_two():
two = [random.randint(1, 100) for x in range(100)]
for x in two:
if x <= 50:
print('Heads')
yield 'Heads'
else:
print('Tails')
yield 'Tails'
coin_1_result = coin_one()
coin_2_result = coin_two()
for coin_1, coin_2 in zip(coin_1_result, coin_2_result):
if coin_1 == 'Heads' and coin_2 == 'Heads':
print('win')
else:
print('loss')
== operator is used to check for equality of two primitive values(int, string)
yield helps in building a list by sending each result to the calling function without returning or exiting the called method (coin_one() or coin_two()).
Here zip(a, b) allows two iterables (Example: list) to be iterate at the same time. ie It will return coin_1_result[0], coin_2_result[0] in first iteration and in second iteration it returns coin_1_result[1], coin_2_result[1] and so on.
Additionally, you can also notice that result of win or loss will be instantly printed after yielding first set of values from both functions.
Sample output:
Heads
Tails
loss
Heads
Heads
win
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.