[Tutor] Tutor Digest, Vol 35, Issue 16

David Seto dseto200 at yahoo.com
Mon Jan 8 03:52:58 CET 2007


Thanks, I finally figured it out. Here it is
#Coin Toss Game
import random

print"This game will simulate 100 coin tosses and then
tell you the number of head's and tails"

tosses=0
heads=0
tails=0

while tosses<100:
  tosses<100
  coin=random.randrange(2)
  tosses+=1
  if coin==0:
     heads +=1
     print "Heads"
  else:
      tails+=1
      print "Tails"

      
print"100 tosses have been simulated. Please wait for
your results"
print "\nOut of",tosses,",",heads,"were heads
and",tails,"were tails."
--- tutor-request at python.org wrote:

> Send Tutor mailing list submissions to
> 	tutor at python.org
> 
> To subscribe or unsubscribe via the World Wide Web,
> visit
> 	http://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body
> 'help' to
> 	tutor-request at python.org
> 
> You can reach the person managing the list at
> 	tutor-owner at python.org
> 
> When replying, please edit your Subject line so it
> is more specific
> than "Re: Contents of Tutor digest..."
> 
> 
> Today's Topics:
> 
>    1. coin toss program without using the for/range
> loop (David)
>    2. Re: coin toss program without using the
> for/range loop
>       (Kent Johnson)
>    3. Re: coin toss program without using the
> for/range loop
>       (Luke Paireepinart)
>    4. Re: coin toss program without using the
> for/range loop
>       (Christopher Arndt)
>    5. Re: Question regarding parsing HTML with
> BeautifulSoup
>       (Shuai Jiang (Runiteking1))
>    6. Re: SPE - Stani's Python Editor ?
> (Shanmuhanathan T)
>    7. Re: Need help with rewriting script to use
> Decimal  module
>       (Terry Carroll)
> 
> 
>
----------------------------------------------------------------------
> 
> Message: 1
> Date: Thu, 4 Jan 2007 14:08:29 -0800
> From: "David" <dseto200 at pacbell.net>
> Subject: [Tutor] coin toss program without using the
> for/range loop
> To: <tutor at python.org>
> Message-ID: <000001c7304c$de339a40$7301a8c0 at davidxp>
> Content-Type: text/plain; charset="us-ascii"
> 
> How to write the coin toss program without using the
> for/range loop program.
> 
> Program flips a coin 100 times and then tells you
> the number of heads and
> tails.
> 
>  
> 
> I can only solve it using the for/range loop
> 
>  
> 
> Import random
> 
>   Heads=0
> 
>   For 1 in range (100):
> 
>   Heads+=random.randrange(2)
> 
>  
> 
> print "Hit heads"+" "+str(heads)+" "+"times"+" " +
> "hit tails" + "
> "+str(100-heads)+" " + "times"
> 
>  
> 
> I don't see how you solve this program just using
> the while loop program and
> if/else statement.
> 
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL:
>
http://mail.python.org/pipermail/tutor/attachments/20070104/8ca7ecfd/attachment-0001.html
> 
> 
> ------------------------------
> 
> Message: 2
> Date: Thu, 04 Jan 2007 19:12:05 -0500
> From: Kent Johnson <kent37 at tds.net>
> Subject: Re: [Tutor] coin toss program without using
> the for/range
> 	loop
> To: dseto200 at pacbell.net
> Cc: tutor at python.org
> Message-ID: <459D97D5.7050800 at tds.net>
> Content-Type: text/plain; charset=windows-1252;
> format=flowed
> 
> David wrote:
> > How to write the coin toss program without using
> the for/range loop program.
> > 
> > Program flips a coin 100 times and then tells you
> the number of heads 
> > and tails.
> > 
> >  
> > 
> > I can only solve it using the for/range loop
> > 
> >  
> > 
> > Import random
> > 
> >   Heads=0
> > 
> >   For 1 in range (100):
> > 
> >   Heads+=random.randrange(2)
> > 
> >  
> > 
> > print ?Hit heads?+? ?+str(heads)+? ?+?times?+? ? +
> ?hit tails? + ? 
> > ?+str(100-heads)+? ? + ?times?
> > 
> >  
> > 
> > I don?t see how you solve this program just using
> the while loop program 
> > and if/else statement.
> 
> This sounds a lot like homework so I won't give you
> the whole answer, 
> but you can write a for loop using while and a
> counter variable.
> 
> Kent
> 
> 
> 
> ------------------------------
> 
> Message: 3
> Date: Thu, 04 Jan 2007 18:41:57 -0600
> From: Luke Paireepinart <rabidpoobear at gmail.com>
> Subject: Re: [Tutor] coin toss program without using
> the for/range
> 	loop
> To: Kent Johnson <kent37 at tds.net>
> Cc: tutor at python.org, dseto200 at pacbell.net
> Message-ID: <459D9ED5.2010209 at gmail.com>
> Content-Type: text/plain; charset=windows-1252;
> format=flowed
> 
> Kent Johnson wrote:
> > David wrote:
> >   
> >> How to write the coin toss program without using
> the for/range loop program.
> >>
> >> Program flips a coin 100 times and then tells you
> the number of heads 
> >> and tails.
> >>
> >>  
> >>
> >> I can only solve it using the for/range loop
> >>
> >>  
> >>
> >> Import random
> >>
> >>   Heads=0
> >>
> >>   For 1 in range (100):
> >>
> >>   Heads+=random.randrange(2)
> >>
> >>  
> >>
> >> print ?Hit heads?+? ?+str(heads)+? ?+?times?+? ?
> + ?hit tails? + ? 
> >> ?+str(100-heads)+? ? + ?times?
> >>
> >>  
> >>
> >> I don?t see how you solve this program just using
> the while loop program 
> >> and if/else statement.
> >>     
> >
> > This sounds a lot like homework so I won't give
> you the whole answer, 
> > but you can write a for loop using while and a
> counter variable.
> >
> >   
> In fact, in most languages a for loop and a while
> loop are very similar:
> 
> for(int i =0; i < 100; i++)
> {
>     //do something
> }
> is the same as
> int i = 0
> while (i < 100)
> {
>     // do something
>     i++;
> }
> 
> The difference in Python comes from the fact that
> the 'for' loop 
> iterates over a list of objects,
> rather than incrementing a variable.
> If you're using a for-range loop in Python you're
> using it in the 
> old-style that C and C++ programs use (other
> languages too)
> in which case you could easily use a while loop as
> well.
> But if you're indexing into a specific list,
> it becomes much more clear why we use python's
> 'for',
> to index directly into that list without having to
> deal with an index 
> variable.
> 
> for x in some_list:
>     // do something with x
> 
> rather than
> 
> index = 0
> while index < len(some_list):
>     x = some_list[index]
>     // do something with x
>     index += 1
> 
> HTH,
> -Luke
> > Kent
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> >   
> 
> 
> 
> ------------------------------
> 
> Message: 4
> Date: Fri, 05 Jan 2007 01:50:36 +0100
> From: Christopher Arndt <chris.arndt at web.de>
> Subject: Re: [Tutor] coin toss program without using
> the for/range
> 	loop
> To: Tutor at python.org
> Message-ID: <459DA0DC.2030907 at web.de>
> Content-Type: text/plain; charset=windows-1252
> 
> Kent Johnson schrieb:
> > David wrote:
> >> print ?Hit heads?+? ?+str(heads)+? ?+?times?+? ?
> + ?hit tails? + ? 
> >> ?+str(100-heads)+? ? + ?times?
> > 
> > This sounds a lot like homework [...]
> 
> An if you want to get extra marks for pythonicity 
> ;-), read about string
> formatting here:
> 
>
http://www.python.org/doc/current/lib/typesseq-strings.html
> 
> Chris
> 
> 
> ------------------------------
> 
> Message: 5
> Date: Thu, 4 Jan 2007 20:58:36 -0500
> From: "Shuai Jiang (Runiteking1)"
> <marshall.jiang at gmail.com>
> Subject: Re: [Tutor] Question regarding parsing HTML
> with
> 	BeautifulSoup
> To: "Kent Johnson" <kent37 at tds.net>
> Cc: Tutor at python.org
> Message-ID:
> 
>
<a678270c0701041758q7d7ba54bg605e4af5d2272e4 at mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
> 
> Hi,
> 
> Wow, thats much more elegant than the idea I thought
> of.
> 
> Thank you very much Kent!
> 
> Marshall
> 
> On 1/3/07, Kent Johnson <kent37 at tds.net> wrote:
> >
> > Shuai Jiang (Runiteking1) wrote:
> > > Hello,
> > >
> > > I'm working on a program that need to parse a
> financial document on the
> > > internet
> > > using BeautifulSoup. Because of the nature of
> the information, it is all
> > > grouped
> > > as a table. I needed to get 3 types of info and
> have succeeded quite
> > > well using
> > > BeautifulSoup, but encountered problems on the
> third one.
> > >
> > > My question is that is there any easy way to
> parse an HTML tables column
> > > easily using BeautifulSoup. I copied the table
> here and I need to
> > > extract the EPS. The numbers are
> > > every sixth one from the  <tr> tag ex 2.27,
> 1.86, 1.61...
> >
> > Here is one way, found with a little experimenting
> at the command prompt:
> >
> > In [1]: data = '''<table id="INCS"
> style="width:580px" class="f10y"
> > cellspacing="0">
> > <snip the rest of your data>
> >     ...: </table>'''
> > In [3]: from BeautifulSoup import BeautifulSoup as
> BS
> >
> > In [4]: soup=BS(data)
> >
> > In [11]: for tr in soup.table.findAll('tr'):
> >     ....:     print tr.contents[11].string
> >     ....:
> >     ....:
> > EPS
> > 2.27
> >   1.86
> > 1.61
> >   1.27
> > 1.18
> >   0.84
> > 0.73
> >   0.46
> > 0.2
> >   0.0
> >
> > Kent
> >
> >
> >
> 
> 
> -- 
> I like pigs. Dogs look up to us. Cats look down on
> us. Pigs treat us as
> equals.
>     Sir Winston Churchill
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL:
>
http://mail.python.org/pipermail/tutor/attachments/20070104/09d685fb/attachment-0001.htm
> 
> 
> ------------------------------
> 
> Message: 6
> Date: Fri, 5 Jan 2007 11:48:50 +0530
> From: "Shanmuhanathan T" <shanmuha at gmail.com>
> Subject: Re: [Tutor] SPE - Stani's Python Editor ?
> To: "Mike Hansen" <Mike.Hansen at atmel.com>
> Cc: tutor at python.org
> Message-ID:
> 
>
<9871e7140701042218yefda759l8e614a51c58f7a2d at mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
> 
> I found eclipse+pydev to be as good as/better than
> komodo or wingware.
> also eclipse has other plugins which makes it a
> powerful IDEs for
> php/ruby/perl etc.
> 
> Regards,
> Shanmu.
> 
> On 1/4/07, Mike Hansen <Mike.Hansen at atmel.com>
> wrote:
> >
> >
> >
> > > -----Original Message-----
> > > From: OkaMthembo [mailto:zebra05 at gmail.com]
> > > Sent: Thursday, January 04, 2007 9:18 AM
> > > To: Mike Hansen
> > > Subject: Re: [Tutor] SPE - Stani's Python Editor
> ?
> > >
> > > have you tried Komodo from ActiveState or
> Wingware? might be
> > > great, comeercial though.
> > >
> > >
> >
> > I convinced my company to purchase Komodo. I use
> it off an on. I mostly
> > gravitate toward VIM. I'm looking forward to the
> next version of Komodo
> > since it will have vi keybindings. The nice thing
> about Komodo is that
> > you can use it for other languages like Perl and
> Ruby where I believe
> > that Wingware is Python only.
> >
> > Mike
> >
> >
> > -------------
> >
> >   NOTICE:  This e-mail transmission and any
> documents or files attached to
> >   it contain information for the sole use of the
> above-identified
> > individual or entity.
> >
> >   Its contents may be privileged, confidential,
> and exempt from disclosure
> > under the law.
> >   Any dissemination, distribution, or copying of
> this communication is
> > strictly prohibited.
> >
> >   Please notify the sender immediately if you are
> not the intended
> > recipient.
> >
> > FGNS
> >
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> >
> >
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL:
>
http://mail.python.org/pipermail/tutor/attachments/20070105/f922f725/attachment.html
> 
> 
> ------------------------------
> 
> Message: 7
> Date: Thu, 4 Jan 2007 23:21:26 -0800 (PST)
> From: Terry Carroll <carroll at tjc.com>
> Subject: Re: [Tutor] Need help with rewriting script
> to use Decimal
> 	module
> To: tutor at python.org
> Message-ID:
> 
>
<Pine.LNX.4.44.0701042304480.30388-100000 at violet.rahul.net>
> Content-Type: TEXT/PLAIN; charset=US-ASCII
> 
> On Wed, 3 Jan 2007, Dick Moores wrote:
> 
> > Be that as it may, farey() is an amazing program. 
> 
> Not to beat this subject to death, but the comment
> at the bottom of 
>
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52317
> about 
> continued fractions piqued my interest.  I'm no
> mathematician, but I 
> encountered continued fractions a long time ago and
> was fascinated by 
> them.  So I read the URL pointed to, 
> http://mathworld.wolfram.com/ContinuedFraction.html
> , and came up with the 
> following:
> 
>
#####################################################
> 
> def cf(x, tol=0.0001, Trace=False):
>     """
>     Calculate rational approximation of x to within
> tolerance of tol;
>     returns a tuple consisting of numerator and
> denominator p/q
>     Trace=True causes iterated results to be shown
>     """
>     a, r, p, q = [], [], [], []
>     Done = False
>     n = 0
>     if Trace: print "x:%f tol:%f" % (x, tol)
>     while not Done:
>         a.append(None)
>         r.append(None)
>         p.append(None)
>         q.append(None)
>         if n == 0: r[n] = x
>         else: r[n] = 1/(r[n-1]-a[n-1])
>         a[n] = int(r[n])
>         if n == 0:
>             p[n] = a[0]
>             q[n] = 1
>         elif n ==1:
>             p[n] = a[n]*p[n-1] + 1
>             q[n] = a[n]
>         else:
>             p[n] = a[n]*p[n-1] + p[n-2]
>             q[n] = a[n]*q[n-1] + q[n-2]
>         if Trace:
>             print "n:%d a:%d p:%d q:%d approx:%f" %
> \
>                   (n, a[n], p[n], q[n],
> float(p[n])/q[n])
>         if abs(float(p[n])/q[n] - x) < tol:
>             Done = True
>         num = p[n]; denom = q[n]
>         n += 1
>     return (num, denom)
> 
>
#####################################################
> 
> Here's a result for pi:
> 
> >>> print cf(3.14159265357989,0.0000001, Trace=True)
> x:3.141593 tol:0.000000
> n:0 a:3 p:3 q:1 approx:3.000000
> n:1 a:7 p:22 q:7 approx:3.142857
> n:2 a:15 p:333 q:106 approx:3.141509
> n:3 a:1 p:355 q:113 approx:3.141593
> n:4 a:292 p:103993 q:33102 approx:3.141593
> (103993, 33102)
> 
> i.e., the first 5 approximations it came up with
> were 3/1, 22/7, 333/106, 
> 355/113 and a whopping 103993/33102.
> 
> For the 0.36 example you used earlier:
> 
> >>> print cf(0.36, .01, Trace= True)
> x:0.360000 tol:0.010000
> n:0 a:0 p:0 q:1 approx:0.000000
> n:1 a:2 p:1 q:2 approx:0.500000
> n:2 a:1 p:1 q:3 approx:0.333333
> n:3 a:3 p:4 q:11 approx:0.363636
> (4, 11)
> >>>
> 
> it went right from 1/3 to 4/11 (0.363636), skipping
> the 3/8 (0.375) option 
> from the farey series.  
> 
> But this continued fraction algorithm is ill-suited
> to answer the question
> "what's the closest fraction with a denominator <
> N", because it doesn't
> try to find that, it jumps further ahead with each
> iteration.
> 
> Anyway, I thought you might find it interesting
> based on our discussion.
> 
> (Yeah, I know, I didn't choose the formats well on
> those print 
> statements.)
> 
> 
> 
> ------------------------------
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 
> End of Tutor Digest, Vol 35, Issue 16
> *************************************
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


More information about the Tutor mailing list