Search code examples
pythondebuggingpython-3.xattributeerror

What am I doing wrong in my coin flip code?


I am trying to write a program that flips a coin, and displays Heads or Tails accordingly. This is what I have:

    from random import random
    while True:

        for i in range (2):
            spin = i.random()
            if spin == 0:
                print ("Heads")
            else:
                print ("Tails")

but I keep getting the error:

Traceback (most recent call last):
  File "C:/Users/Yamnel/Desktop/Python Programs/coin flip.py", line 5, in     <module>
    spin = i.random()
AttributeError: 'int' object has no attribute 'random'

Solution

  • import random
    
    while True:
        result = random.randrange(2)
        if result == 0:
           print ("Heads")
        else:
           print ("Tails")