Hi Pythonistas

I am having difficulty with applying the list.append(x) method to produce a list that will contain outputs which will become coordinates for a later call to Matplotlib. Perhaps someone here can help me figure this out?

The basic program is below:

# St Petersburg Game: v. 2:
# Toss a coin.  If it is heads, win $2, if not keep
#   tossing it until it falls heads.
#   Heads first toss = H = $2
#   Heads third toss = TTH = $8
#   Heads fifth toss = TTTTH = $32

# The game is to win more by not scoring Heads

print """St Petersburg Game: win multiples of $2 the
more you land Tails"""

# Required libraries
import random
import matplotlib.pyplot as plt

#Main function:
def flipCoin():
   coinToss = random.randrange(1, 3)
   return coinToss

# Storage of output
toss_list = []

# Get things going
flipCoin()

# Want to capture the coin lands heads (2)
while flipCoin() != 2:
   toss_list.append("Tails")
   flipCoin()

# Heads lands & show output print
print "Heads"

print toss_list

# Interpret results & 'reward'
print "You flipped %d tails before landing Heads" % len(toss_list)

if toss_list == 0:
   print "You won $2"

else:
   toss_list.append( "Tail" )
   print "You won $%d" % 2 ** len(toss_list)



The overall purpose of the game is, for this discussion, irrelevant, but some background info will be helpful I think. The above program will give one run only and produces the output I expect. When I take this to the next level of complexity I run into problems.

1. I have tried to make this program run a given number of times, and use the for repetition loop to do this, basically:

for i  in range( 0, 10 ):

and then the above program is appropriately indented.

2. Collecting the number of coin "tosses" into a list appends these to a list just fine. However, what this does is adds the numbers together so that one ends up like this:

[0, 1, 2, 4, 5, 6, 8, 10, 11, 15]

With a corresponding increase in the values derived from multiplying the exponent, thus:

[2, 4, 8, 32, 64, 128, 512, 2048, 4096, 65536]

Both are correct applications of the method, but I am unable to get the list to not sum the values up in the first list, these are not accumulative values, but discrete. If I am understanding what is currently happening, the values are being accumulated, and I want to stop that from happening.

If this isn't clear, please let me know how I can clarify my question to help shape the relevance of the responses.

Thanks for any ideas.

AG
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to