Re: [Tutor] List append method: St Petersburg Game

2010-02-20 Thread AG

bob gailer wrote:

On 2/20/2010 7:43 AM, AG wrote:





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()




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

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


Re: [Tutor] List append method: St Petersburg Game

2010-02-20 Thread bob gailer

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

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


[Tutor] List append method: St Petersburg Game

2010-02-20 Thread AG

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 = H = $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