[Tutor] List append method: St Petersburg Game

AG computing.account at googlemail.com
Sat Feb 20 16:09:26 CET 2010


bob gailer wrote:
> On 2/20/2010 7:43 AM, AG wrote:
>> <snip>
>
>
> Please let me know how I can clarify my question
>
> 1 - You are giving way too much information. We do not need to know 
> the rules of the game or all the code. Our time to read email is 
> limited. The less you tell us that is not relevant the better. 
Thanks Bob.

> Also you don't show the code for the "next level of complexity". 

Here it is, then:

import random
import matplotlib.pyplot as plt
import math

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

toss_list = []
tosscounts = []
winnings = []


for i in range(0, 10):

    while flipCoin() != 2:
        toss_list.append("Tails")
        flipCoin()


    print
    print "Heads"


    tosscounts.append( len(toss_list))

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

    else:
        toss_list.append( "Tail" )

        winnings += [2 ** len( toss_list )]
   

print
print tosscounts
print winnings

print "Here's the graph: "

for i in winnings:              # Convert int to float for log
    i * 1.0
   
plt.plot( [tosscounts], [winnings] )
plt.ylabel( "how often" )
plt.xlabel( "how much" )
plt.show()

<snip>
>
> The result of the first call to flipCoin is ignored.
> Each cycle of the loop results in 2 calls to flipCoin. The result of 
> the 2nd call is ignored.
>
Aha!  Thanks for spotting that.  Now fixed in the code cited above, but 
still gives the same problem.

Thanks for any further ideas.

AG



More information about the Tutor mailing list