[Tutor] Whle Loop to run again.

Ricardo Catalinas Jimenez th1nk3r at server01.org
Thu May 19 17:22:57 CEST 2005


0n Thu, May 19, 2005 at 02:50:16PM +0000, . , wrote:
> Hi,
> 
> I want the program to run again when i input 'run' But, It doesn't...
> 
> thanks.
> 
> -------------------------------------------------------------------------------------------------------------------------------------------
> import random
> 
> raw_input("Flip a coin!")
> 
> head = 0
> tail = 0
> run = ""
> 
> while (head + tail) < 100:
>     coin = random.randrange(2)
>     if coin == 0:
>         head += 1
> 
>     elif coin == 1:
>         tail += 1
> 
>     elif run == "run":
>         continue
> 
>     elif run == "ex1t":
>         print ex1t
>         break
> 
>     else:
>         print "Error"
> 
> print "\n", head, "heads,", tail, "tails out of 100."
> 
> 
> 
> 
> run = raw_input("\n'run' again or 'exit' ")
> 
> ex1t = raw_input("\nPress the enter key to exit.")

--- code ---

import random

raw_input("Flip a coin!")

head = 0
tail = 0
run = ""

while True:
	while (head + tail) < 100:
		coin = random.randrange(2)
		if coin == 0:             
			head += 1
               
		elif coin == 1:
			tail += 1
                
	print "\n", head, "heads,", tail, "tails out of 100."
	head, tail = 0, 0


	run = raw_input("\n'run' again or 'exit' ")
	if run == "exit":
		break
	
	elif run == "run":
		continue

	else:
		print "Error"
	

ex1t = raw_input("\nPress the enter key to exit.")

--- end code ---

Sorry, I changed your indentation by tabs characters.
-- 
Ricardo Catalinas Jimenez    Madrid, Spain.
th1nk3r(at)server01(dot)org  http://www.server01.org
GnuPG Key fingerprint = 662C EBF7 9220 0F14 C644  F90B DC3E A163 7F8D CDAE


More information about the Tutor mailing list