[Tutor] Whle Loop to run again.

Alan G alan.gauld at freenet.co.uk
Thu May 19 22:52:56 CEST 2005


> I want the program to run again when i input 'run' But, It
doesn't...

You need another loop, or to ask for input inside the loop.
If you always want 100 iterations then prompt for another
hundred structure it like:

run = raw_input('run or exit? ')
while run = 'run':
   for n in range(100):
      coin = random.randrange(2)
      if coin == 1: heads += 1
      else coin == 2: tails += 1
   run = raw_input('run or exit? ')


But if you want to check after every coin toss (as your if/elif chain
code
seems to suggest) but with a maximum of 100 iterations, try it this
way:

heads,tails = 0,0
while heads+tails < 100:
     coin = random.randrange(2)
     if coin == 1: heads += 1
     else coin == 2: tails += 1

     run = raw_input('run or exit? ')
     if run = 'run': continue
     else: break

HTH,

Alan G.



More information about the Tutor mailing list