pythondouble-quotescoin-flipping

double quote coin flip


import random
input("You tossed a coin. Select 0 or 1\n" )
random_side = random.randint(0, 1)
if random_side == 1:
  print("Heads")
else:
  print("Tails")    

when I use double quote in 1(if random_side == "1":). It gives me same output not random outcome.


Solution

  • Quoting the result of random.randint won't work because randint produces an integer value. You can tell the type of a value by calling the type function:

    >>> type(random.randint(0, 1))
    <class 'int'>
    

    So, when comparing, you should do if if random_side == 1:. Alternatively, you could do if random_side: as Python will interpret a 0 as False and a 1 as True.

    As an aside, before using Python's random library, you should seed the random generator to ensure that you get reasonably random results. Failing to do so might result in the random number generator using the same values for each run. You can find relevant documentation here.