[Tutor] new user question about while loops

Norman Silverstone norman at littletank.org
Wed Oct 26 12:19:14 CEST 2005


This may not be up to the standard of the more experienced programmer
but here is my effort. An added bit is the ability to go on using the
code until you are fed up.

#To check the computers odds and evens
import random
anothergo = "y"
while(anothergo != "n"):
  

   oddsevens = random.randrange(2)
   odd = 0
   even = 0
   tries = 0
   
   while(tries != 100):
      oddevens = random.randrange(2)
      if (oddevens == 0):
         even += 1
      else:
         odd += 1
      tries += 1      
   print "In %d tries there were %d odds and %d evens" % (tries, odd,
even)
   
   anothergo = raw_input("Have another go y/n - ")  


Hope you like it.

Norman 




More information about the Tutor mailing list