[Tutor] List append method: St Petersburg Game

bob gailer bgailer at gmail.com
Sat Feb 20 15:49:43 CET 2010


On 2/20/2010 7:43 AM, AG wrote:
> 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?


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. Also you don't 
show the code for the "next level of complexity". What you should show 
us is:
> for i  in range( 0, 10 ):
some lists are initialized and appended to. What are they and how are 
they appended?
>
>    #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()

2 - The most obvious problem is here:

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

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.


> 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. 

Then your expectation is misguided, given my comments regarding multiple 
calls to flipCoin! You don't actually know how many tosses were made!

[snip]

-- 
Bob Gailer
919-636-4239
Chapel Hill NC



More information about the Tutor mailing list