0

I am fairly new to python and was wondering how to make this loop run for a the number of iterations that is entered by the user, however it is an infinite loop at the moment:

def randMod():
   import random
   heads = 0
   tails = 0
   tries = raw_input('Enter a number:')

   while True:
       runs = 0
       if tries == runs:
           break
       else:
           runs + 1


       coinFlip = random.randrange(0,1+1)

       if coinFlip == 0:
           print "Tails"
           tails + 1

       elif coinFlip == 1:
           print "Heads"
           heads + 1

       print heads
       print tails

randMod()

I am trying to make it so it will simulate a coin flip for how many times the user enters then tallies it at the end. Only problem is I am fairly new to python so I don't know if I got this right or not.

1 Answers1

1

The problem I see here is that you are using raw_input() to read the user's input. That method stores the input as a string. You must convert the information contained in tries to a number in order for this to work. As it is comparing tries == runs and a string will never be equal to a int, it is stuck forever.

You can use the conversion like this: Explained here

Community
  • 1
  • 1
  • Minor nitpick: it's not casting (re-interpreting a value as a different type) it's conversion (taking a value of one type and creating a value of another type) – Wes Oct 13 '13 at 15:10