-1
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.

3 Answers3

0

If you wanted your input to determine which side the coin flips, then you have to first:

  1. store the input in a variable

  2. since you want it to be an integer, ensure it is an integer

  3. add it to the conditional

    coinFlip = int(input("You tossed a coin. Select 0 or 1\n"))

    if coinFlip == 1: print("Heads") else: print("Tails")

NJAS
  • 53
  • 5
0

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.

Woody1193
  • 7,252
  • 5
  • 40
  • 90
0

A much simpler way to choose heads or tails would be random.choice(). See below:

from random import choice
print(choice(['heads', 'tails'])) # Or assign this to a variable

Please read up on the random modules.

Codeman
  • 477
  • 3
  • 13