[Tutor] Maths operations on lists

Michael Barrett mike@daboyz.org
Tue Jun 10 16:17:00 2003


Assuming you have a list of the # of guesses per game called numOfGuesses, here's what I'd do.

# Assume numOfGuesses = [ 4, 6, 10, 5 ]

>>> numOfGuesses = [ 4, 6, 10, 5 ]
>>> totalGuesses = 0
>>> for guess in numOfGuesses:
...     totalGuesses = totalGuesses + guess
... 
>>> print "Total # of guesses: %d" % ( totalGuesses )
Total # of guesses: 25
>>> print "Total # of games: %d" % ( len(numOfGuesses) )
Total # of games: 4
>>> print "Average guesses per game: %f" % ( float(totalGuesses) / float(len(numOfGuesses)) )
Average guesses per game: 6.250000


=======

Hope that helps.

On Tue, Jun 10, 2003 at 08:57:09PM +0100, valhalla wrote:
> Hello Pythonites
> 
> This may be a no-brainer, but after looking at several docs on lists I cannot 
> get my head around the proper way of doing this, so I would appreciate some 
> help.
> 
> I'm working on a pretty basic guessing game, but wanted to keep a tally of the 
> number of times it takes the player to guess the correct number and then to 
> average that tally out over the number of games, to yield an output along the 
> lines of: "You took an average of 5 guesses", or whatever.
> The inputs would be:
> (a) the number of guesses per game which I have appended to a list
> (b) the total number of games played
> and the output
> (a) divided by (b) == (c) average
> 
> I *think* what I am after is a way of adding the contents of a list and then 
> dividing that total to yield an average - but I'm not sure (i) whether one 
> can do those operations with lists and if so, how or (ii) if not, what the 
> next best thing would be, given that there has to be a data structure to 
> catch the number of guesses per game. 
> 
> Any ideas/clues?
> 
> Cheers
>  
> ====================
> ||  Reg. Linux User: 313143 ||
> ====================
> Sign the Petition:
> http://www.PetitionOnline.com/endtcpa1/
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
     ________________________________________________________________________
                Mike Barrett | "We have veggie bacon, why don't we have
             mike@daboyz.org |  meat fruit?"
              www.daboyz.org |    -- My co-worker, Ben
     ------------------------+-----------------------------------------------