I want to tell the user that they are very lucky if they get heard or tail 6 times in a row

74 Views Asked by At

I am trying to create a Coin Flip Game this is what I have came up with so far:

import random

def headsOrTails(number_of_flips):
  number_of_flips = int(input("How many times do you want to flip the coin: "))
  heards_count = 0
  tails_count = 0
  for i in range(number_of_flips):
    rand = random.randint(1, 2)
    if rand == 1:
      heards_count += 1
      print(f"It is Heads.\n Heads {heards_count}")
    elif rand == 2:
      tails_count += 1
      print(f"It is Tails.\n Tails {tails_count}")
  print(heards_count)
  print(tails_count)
headsOrTails(1)

I want to tell the user that they are very lucky if they get heard or tail 6 times in a role. And I was wondering if anyone can help me do that.

3

There are 3 best solutions below

1
EveningTangerine On

An if statement like this should work:

if heards_count == 6:
     print('You are very lucky!')

heards_count can only equal 6 if you pass in six to the function headsOrTails(). Get rid of the line

if rand == 1 and rand == 1 and rand == 1 and rand == 1:

rand will be reset on each iteration of the loop so it does not check if rand has been equal to 1 four times. Hope this helps!

0
Lane Dalan On

Here is what I would do. I would use the heads_count and the tails_count as a heads/tails in a row count. If you get a heads, reset the tails count. If you get a tails, reset the heads count.

import random


def headsOrTails():
    number_of_flips = int(input("How many times do you want to flip the coin: "))
    heads_count = 0
    tails_count = 0
    for i in range(number_of_flips):
        rand = random.randint(1, 2)
        if rand == 1:
            tails_count = 0
            heads_count += 1
            print(f"It is Heads.\n Heads in a row {heads_count}")
        elif rand == 2:
            heads_count = 0
            tails_count += 1
            print(f"It is Tails.\n Tails in a row {tails_count}")

        if heads_count == 6 or tails_count == 6:
            print("You are very lucky")


headsOrTails()
0
patate1684 On
import random

def headsOrTails(n):
    heads_count = 0
    tails_count = 0
    heads_inarow = 0
    tails_inarow = 0
    for i in range(n):
        rand = random.randint(1, 2)
        if rand == 1:
            tails_inarow = 0
            heads_inarow += 1
            heads_count += 1
            print(f"It is Heads.\n Heads in a row {heads_inarow}")
        elif rand == 2:
            heads_inarow = 0
            tails_inarow += 1
            tails_count += 1
            print(f"It is Tails.\n Tails in a row {tails_inarow}")

        if heads_inarow == 6 or tails_inarow == 6:
            print("You are very lucky")
    print(heards_count)
    print(tails_count)

number_of_flips = int(input("How many times do you want to flip the coin: "))
headsOrTails(number_of_flips)