0

I am creating a game with coinflips. When all 100 coins are finished flipping I would like to calculate the percentage. The code provided below however, will either display "hp" or "tp" as 100 even though its obviously not 100%.

import random

flips=100
heads=0
tails=0

while flips!=0:

    coinflip=random.randint(0,1)

    if coinflip==0:
        heads+=1
    elif coinflip==1:
        tails+=1     
    flips-=1

print "Heads - "+str(heads)
print "Tails - "+str(tails)
print "-----------"
hp=float((tails/heads)*100)
tp=float((heads/tails)*100)

I might be the way python processes things? or maybe I'm just stupid and cant do math.

Gordon Linoff
  • 1,122,135
  • 50
  • 484
  • 624
Jmilicev
  • 73
  • 1
  • 1
  • 8

1 Answers1

2

Division in python2 is by default not floating point.

At the beginning of your code add an import

from __future__ import division

This adopts Python3's behavior

Previous Answer

Jerry Zhao
  • 184
  • 11