[Tutor] Need help solving this problem

David david at abbottdavid.com
Wed Jun 10 23:28:16 CEST 2009

Raj Medhekar wrote:
> I have been teaching myself Python using a book. The chapter I am on 
> currently, covers branching, while loops and program planning. I am 
> stuck on on of the challenges at the end of this chapter, and I was 
> hoping to get some help with this. Here it is:
> Write a program that flips a coin 100 times and the tells you the number 
> of heads and tails.
> I have tried to think about several ways to go about doing this but I 
> have hit a wall. Even though I understand the general concept of 
> branching and looping, I have been having trouble writing a program with 
> these.  I look forward to your reply that will help me understand these 
> structures better.
> Sincerely,
> Raj
> ------------------------------------------------------------------------
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
I am learning also, I came up with;


import random
random = random.Random()
choice = ['heads', 'tails']
head_wins = 0
tail_wins = 0

def coin_flip():
     flip = random.choice(choice)
     global head_wins
     global tail_wins
     if flip == 'heads':
         head_wins += 1
     elif flip == 'tails':
         tail_wins += 1

def number_flips():
     for i in range(100):

print 'Head Total Wins =', head_wins
print 'Tail Total Wins =', tail_wins

Powered by Gentoo GNU/Linux

More information about the Tutor mailing list