0

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?

  • `print('win') if random.randint(0,1) == random.randint(0,1) else print('lose')` – Patrick Haugh Feb 03 '17 at 17:56
  • Start with a function that tosses two coins at the same time. At the moment you have two different functions, which both toss 100 coins separately and prints the results. – Tom Dalton Feb 03 '17 at 17:58
  • It might help you think about it to create a single function that tosses a single coin and returns "heads" or "tails". Then other functions you write can use that function instead of having to do less readable things with `random.randint()` – Tom Dalton Feb 03 '17 at 18:00
  • '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.' – Tupilwe Sinyangwe Feb 03 '17 at 18:00

3 Answers3

1

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).
ospahiu
  • 3,465
  • 2
  • 13
  • 24
  • We have three outcomes for the experiment: if we get heads on both coins we win (both 1's), if we get tails on both coins we lose (both 0's) and finally 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) – Tupilwe Sinyangwe Feb 03 '17 at 18:33
  • Update to make it more clear which control flow branch belongs to what outcome. @TupilweSinyangwe – ospahiu Feb 03 '17 at 18:40
0

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

Ashish Bainade
  • 426
  • 5
  • 11
  • Really? First, there is no need of writing two functions. Second, you should write your answer using formatted code-blocks. – Peaceful Feb 03 '17 at 18:42
0

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. i.e. 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

Kalai selvan
  • 63
  • 10