[Tutor] Re: Tutor digest, Vol 1 #2559 - 14 msgs Readline code

Cliff Martin camartin@snet.net
Tue Jul 1 11:36:15 2003


--------------020509010608030901030803
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

Rick,

Thanks for your response. I see that, at least for the print statement 
this is true but if I run the code of  your second example and ask for 
the len(words) I only get the word length of the words in the last line. 
 Why aren''t all the elements in the assignment words? I'm sure this is 
trivial but I'm certainly missing it.  Thanks again.

Cliff Martin

tutor-request@python.org wrote:

>Send Tutor mailing list submissions to
>	tutor@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@python.org
>
>You can reach the person managing the list at
>	tutor-admin@python.org
>
>When replying, please edit your Subject line so it is more specific
>than "Re: Contents of Tutor digest..."
>
>
>Today's Topics:
>
>   1. Re: newbie 'while' question (Zak Arntson)
>   2. Re: newbie 'while' question (Danny Yoo)
>   3. Re: newbie 'while' question (Danny Yoo)
>   4. Re: Re: Convert list to literal string. (Derrick 'dman' Hudson)
>   5. Re: newbie 'while' question (Matthew Richardson)
>   6. Re: newbie 'while' question (Zak Arntson)
>   7. readlines code (Cliff Martin)
>   8. writing a search engine (Kyle Babich)
>   9. Re: readlines code (Rick Pasotto)
>  10. Re: writing a search engine (Sean 'Shaleh' Perry)
>  11. Re: finding factorials (Payal Rathod)
>  12. compiling python apps (Michael Honeyfield)
>  13. Re: finding factorials - hmm..., gcd (glingl)
>  14. =?iso-8859-1?Q?audiofile_manipulation?= (=?iso-8859-1?Q?marta=5Fandrea?=)
>
>--__--__--
>
>Message: 1
>Date: Mon, 30 Jun 2003 15:41:40 -0700 (PDT)
>Subject: Re: [Tutor] newbie 'while' question
>From: "Zak Arntson" <zak@harlekin-maus.com>
>To: <tutor@python.org>
>
>  
>
>>Which works great.  I'm still stuck on how to do Zak's method of
>>manipulating the string, but starting to feel better about 'while' now.
>>
>>Thanks,
>>Matt
>>    
>>
>
>Here's some code, missing stuff:
>
>s = raw_input('>')
>while s:    # when will the while loop be done?
>    print s[??]   # ?? is some value
>    s = ?? # you want to change s
>
>---
>Zak Arntson
>www.harlekin-maus.com - Games - Lots of 'em
>
>
>
>
>--__--__--
>
>Message: 2
>Date: Mon, 30 Jun 2003 15:43:37 -0700 (PDT)
>From: Danny Yoo <dyoo@hkn.eecs.berkeley.edu>
>To: Matthew Richardson <marichar@csusb.edu>
>cc: Python Tutor <tutor@python.org>
>Subject: Re: [Tutor] newbie 'while' question
>
>
>
>  
>
>>>In mathematical terms, we'd say that we'd like a half-open range,
>>>
>>>
>>>         0  <=  x   < length of "Matthew Ricahrdson"
>>>
>>>
>>>And we'll find that the equivalent expression in Python is very similar to
>>>this.  *grin*
>>>
>>>
>>>I hope this gives some hints on how to correct the while 'condition'
>>>expression.  Good luck!
>>>      
>>>
>>I knew I should have paid more attention in math...
>>    
>>
>
>
>You did have the right approach.  That is, you approached it from the
>left:
>
>    while x >= 0:                   #  0 <= x
>
>You just had to have the right approach.  *grin*
>
>    while x < len(s):               #  x < len(s)
>
>
>
>
>
>By the way, it turns out that we can do both directions at the same time:
>
>    while 0 <= x < len(s):          #  0 <= x and x < len(s)
>
>
>which is a safe way to go about it.  Of course, only a few programmers I
>know will ever write the condition like this.  *grin* Most will just
>write:
>
>    x < len(s)
>
>and just assume that x is nonnegative.  Crazy programmers.  *grin*
>
>
>
>Good luck to you!
>
>
>
>--__--__--
>
>Message: 3
>Date: Mon, 30 Jun 2003 15:53:24 -0700 (PDT)
>From: Danny Yoo <dyoo@hkn.eecs.berkeley.edu>
>To: Eric L Howard <elh@outreachnetworks.com>
>cc: Python Tutor <tutor@python.org>
>Subject: Re: [Tutor] newbie 'while' question
>
>
>
>  
>
>>>This is very close to the correct solution.  You get the number of
>>>characters in the string with the len() built-in function.  But
>>>
>>>  s = raw_input('Enter a name: ')
>>>  x = 0
>>>  while x <= len(s):
>>>      print s[x]
>>>      x += 1
>>>
>>>will still result in an IndexError (even though it's close to being
>>>correct).  Do you see why?
>>>      
>>>
>>[newbie swing ;-)]
>>
>>I know it works, but would it be bad to do something like
>>
>>s = raw_input('Enter a name: ')
>>    for x in range(0, len(s)):
>>    print s[x]
>>
>>There's no need to initialize x, or declare our math statement in the
>>code. Thoughts, corrections?
>>    
>>
>
>
>Hi Eric,
>
>
>Sure, this works great, and you're right: the 'for' loop is the more
>appropriate looping construct here, since we're going along the letters of
>the name.
>
>
>We can even do:
>
>###
>s = raw_input('Enter a name: ')
>for ch in s:
>    print ch
>###
>
>and skip the manual indicing altogether.  *grin*
>
>
>Strings are like Lists --- they are both "sequences" --- and the 'for'
>loop can deal with them uniformly.  Matt's question asked to do it with
>'while', but in real life code, the 'for' loop is usually easier to
>construct because it's less open-ended than 'while'.
>
>
>Good luck!
>
>
>
>--__--__--
>
>Message: 4
>Date: Mon, 30 Jun 2003 18:53:37 -0400
>From: "Derrick 'dman' Hudson" <dman@dman13.dyndns.org>
>To: tutor@python.org
>Subject: Re: [Tutor] Re: Convert list to literal string.
>
>
>--vEao7xgI/oilGqZ+
>Content-Type: text/plain; charset=us-ascii
>Content-Disposition: inline
>Content-Transfer-Encoding: quoted-printable
>
>On Sat, Jun 28, 2003 at 10:00:04AM -0500, David wrote:
>
>| I can make a loop and process the info.
>
>Dealing with the list of data is certainly doable.  You still didn't
>provide the code that shows how you ended up with a list (how!?
>command line arguments are strings until you parse them into something
>else) so I can't explain what you are doing wrong.
>
>Abel already reminded you of
>    ','.join( ['IBM', 'EK', 'MO', 'UTX'] )
>
>(if this helps you, then I suspect you called .split() on the command
>line argument, and that's where your list came from)
>
>[... lots and lots of snip ...]
>| --addstocks  IBM,EK,MO,UTX
>
>This is easy.  See sample (but untested) code below :
>
>
>import sys
>import getopt
>
>try:
>    opts, args =3D getopt.getopt(sys.argv[1:], "", ["addstocks=3D"])
>except getopt.GetoptError:
>    # print help information and exit:
>    usage()     # Note: you have to define this function!
>    sys.exit(2)
>
>stock_labels =3D None
>for o, a in opts:
>    if o =3D=3D "--addstocks" :
>        stock_labels =3D a
>
>if not stock_labels :
>    usage()
>    sys.exit(3)
>
>print stock_labels
>print type(stock_labels)
>print stock_labels.__class__  # use this with python >=3D 2.2
>addstock( stock_labels )
>
>
>-D
>
>--=20
>A violent man entices his neighbor
>and leads him down a path that is not good.
>        Proverbs 16:29
>=20
>http://dman13.dyndns.org/~dman/
>
>--vEao7xgI/oilGqZ+
>Content-Type: application/pgp-signature
>Content-Disposition: inline
>
>-----BEGIN PGP SIGNATURE-----
>Version: GnuPG v1.0.6 (GNU/Linux)
>Comment: For info see http://www.gnupg.org
>
>iEYEARECAAYFAj8Av3EACgkQiB6vp1xAVUAiuwCeNkrIAAt2rnOS68eJ7w4Y92h2
>MIoAnjnLumBlZlLlNUqGY6BCZeprMnBu
>=7tgh
>-----END PGP SIGNATURE-----
>
>--vEao7xgI/oilGqZ+--
>
>
>--__--__--
>
>Message: 5
>Date: Mon, 30 Jun 2003 16:04:14 -0700
>From: Matthew Richardson <marichar@csusb.edu>
>Subject: Re: [Tutor] newbie 'while' question
>To: Danny Yoo <dyoo@hkn.eecs.berkeley.edu>
>Cc: Eric L Howard <elh@outreachnetworks.com>, Python Tutor <tutor@python.org>
>Organization:
>
>On Mon, 2003-06-30 at 15:53, Danny Yoo wrote:
>  
>
>>>>This is very close to the correct solution.  You get the number of
>>>>characters in the string with the len() built-in function.  But
>>>>
>>>>  s = raw_input('Enter a name: ')
>>>>  x = 0
>>>>  while x <= len(s):
>>>>      print s[x]
>>>>      x += 1
>>>>
>>>>will still result in an IndexError (even though it's close to being
>>>>correct).  Do you see why?
>>>>        
>>>>
>>>[newbie swing ;-)]
>>>
>>>I know it works, but would it be bad to do something like
>>>
>>>s = raw_input('Enter a name: ')
>>>    for x in range(0, len(s)):
>>>    print s[x]
>>>
>>>There's no need to initialize x, or declare our math statement in the
>>>code. Thoughts, corrections?
>>>      
>>>
>>Hi Eric,
>>
>>
>>Sure, this works great, and you're right: the 'for' loop is the more
>>appropriate looping construct here, since we're going along the letters of
>>the name.
>>
>>
>>We can even do:
>>
>>###
>>s = raw_input('Enter a name: ')
>>for ch in s:
>>    print ch
>>###
>>
>>and skip the manual indicing altogether.  *grin*
>>
>>
>>Strings are like Lists --- they are both "sequences" --- and the 'for'
>>loop can deal with them uniformly.  Matt's question asked to do it with
>>'while', but in real life code, the 'for' loop is usually easier to
>>construct because it's less open-ended than 'while'.
>>
>>
>>Good luck!
>>
>>    
>>
>
>The exercise is out of Wesley Chun's 'Core Python Programming.'  The for
>loop was far easier for me, but the exercise was to perform the same
>task both ways.  No point in skipping over the 'hard' material in the
>name of expediency.  I want to get a firm grip on this and ploughing
>through all of the examples is the only way I'll get it.
>
>Now for Zak's string manipulation....
>
>Matt
>
>  
>


--------------020509010608030901030803
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <title></title>
</head>
<body>
Rick,<br>
<br>
Thanks for your response. I see that, at least for the print statement this
is true but if I run the code of &nbsp;your second example and ask for the len(words)
I only get the word length of the words in the last line. &nbsp;Why aren''t all
the elements in the assignment words? I'm sure this is trivial but I'm certainly
missing it. &nbsp;Thanks again.<br>
<br>
Cliff Martin<br>
<br>
<a class="moz-txt-link-abbreviated" href="mailto:tutor-request@python.org">tutor-request@python.org</a> wrote:<br>
<blockquote type="cite"
 cite="mid20030701094304.20150.86113.Mailman@mail.python.org">
  <pre wrap="">Send Tutor mailing list submissions to
	<a class="moz-txt-link-abbreviated" href="mailto:tutor@python.org">tutor@python.org</a>

To subscribe or unsubscribe via the World Wide Web, visit
	<a class="moz-txt-link-freetext" href="http://mail.python.org/mailman/listinfo/tutor">http://mail.python.org/mailman/listinfo/tutor</a>
or, via email, send a message with subject or body 'help' to
	<a class="moz-txt-link-abbreviated" href="mailto:tutor-request@python.org">tutor-request@python.org</a>

You can reach the person managing the list at
	<a class="moz-txt-link-abbreviated" href="mailto:tutor-admin@python.org">tutor-admin@python.org</a>

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Tutor digest..."


Today's Topics:

   1. Re: newbie 'while' question (Zak Arntson)
   2. Re: newbie 'while' question (Danny Yoo)
   3. Re: newbie 'while' question (Danny Yoo)
   4. Re: Re: Convert list to literal string. (Derrick 'dman' Hudson)
   5. Re: newbie 'while' question (Matthew Richardson)
   6. Re: newbie 'while' question (Zak Arntson)
   7. readlines code (Cliff Martin)
   8. writing a search engine (Kyle Babich)
   9. Re: readlines code (Rick Pasotto)
  10. Re: writing a search engine (Sean 'Shaleh' Perry)
  11. Re: finding factorials (Payal Rathod)
  12. compiling python apps (Michael Honeyfield)
  13. Re: finding factorials - hmm..., gcd (glingl)
  14. =?iso-8859-1?Q?audiofile_manipulation?= (=?iso-8859-1?Q?marta=5Fandrea?=)

--__--__--

Message: 1
Date: Mon, 30 Jun 2003 15:41:40 -0700 (PDT)
Subject: Re: [Tutor] newbie 'while' question
From: "Zak Arntson" <a class="moz-txt-link-rfc2396E" href="mailto:zak@harlekin-maus.com">&lt;zak@harlekin-maus.com&gt;</a>
To: <a class="moz-txt-link-rfc2396E" href="mailto:tutor@python.org">&lt;tutor@python.org&gt;</a>

  </pre>
  <blockquote type="cite">
    <pre wrap="">Which works great.  I'm still stuck on how to do Zak's method of
manipulating the string, but starting to feel better about 'while' now.

Thanks,
Matt
    </pre>
  </blockquote>
  <pre wrap=""><!---->
Here's some code, missing stuff:

s = raw_input('&gt;')
while s:    # when will the while loop be done?
    print s[??]   # ?? is some value
    s = ?? # you want to change s

---
Zak Arntson
<a class="moz-txt-link-abbreviated" href="http://www.harlekin-maus.com">www.harlekin-maus.com</a> - Games - Lots of 'em




--__--__--

Message: 2
Date: Mon, 30 Jun 2003 15:43:37 -0700 (PDT)
From: Danny Yoo <a class="moz-txt-link-rfc2396E" href="mailto:dyoo@hkn.eecs.berkeley.edu">&lt;dyoo@hkn.eecs.berkeley.edu&gt;</a>
To: Matthew Richardson <a class="moz-txt-link-rfc2396E" href="mailto:marichar@csusb.edu">&lt;marichar@csusb.edu&gt;</a>
cc: Python Tutor <a class="moz-txt-link-rfc2396E" href="mailto:tutor@python.org">&lt;tutor@python.org&gt;</a>
Subject: Re: [Tutor] newbie 'while' question



  </pre>
  <blockquote type="cite">
    <blockquote type="cite">
      <pre wrap="">In mathematical terms, we'd say that we'd like a half-open range,


         0  &lt;=  x   &lt; length of "Matthew Ricahrdson"


And we'll find that the equivalent expression in Python is very similar to
this.  *grin*


I hope this gives some hints on how to correct the while 'condition'
expression.  Good luck!
      </pre>
    </blockquote>
    <pre wrap="">I knew I should have paid more attention in math...
    </pre>
  </blockquote>
  <pre wrap=""><!---->

You did have the right approach.  That is, you approached it from the
left:

    while x &gt;= 0:                   #  0 &lt;= x

You just had to have the right approach.  *grin*

    while x &lt; len(s):               #  x &lt; len(s)





By the way, it turns out that we can do both directions at the same time:

    while 0 &lt;= x &lt; len(s):          #  0 &lt;= x and x &lt; len(s)


which is a safe way to go about it.  Of course, only a few programmers I
know will ever write the condition like this.  *grin* Most will just
write:

    x &lt; len(s)

and just assume that x is nonnegative.  Crazy programmers.  *grin*



Good luck to you!



--__--__--

Message: 3
Date: Mon, 30 Jun 2003 15:53:24 -0700 (PDT)
From: Danny Yoo <a class="moz-txt-link-rfc2396E" href="mailto:dyoo@hkn.eecs.berkeley.edu">&lt;dyoo@hkn.eecs.berkeley.edu&gt;</a>
To: Eric L Howard <a class="moz-txt-link-rfc2396E" href="mailto:elh@outreachnetworks.com">&lt;elh@outreachnetworks.com&gt;</a>
cc: Python Tutor <a class="moz-txt-link-rfc2396E" href="mailto:tutor@python.org">&lt;tutor@python.org&gt;</a>
Subject: Re: [Tutor] newbie 'while' question



  </pre>
  <blockquote type="cite">
    <blockquote type="cite">
      <pre wrap="">This is very close to the correct solution.  You get the number of
characters in the string with the len() built-in function.  But

  s = raw_input('Enter a name: ')
  x = 0
  while x &lt;= len(s):
      print s[x]
      x += 1

will still result in an IndexError (even though it's close to being
correct).  Do you see why?
      </pre>
    </blockquote>
    <pre wrap="">[newbie swing ;-)]

I know it works, but would it be bad to do something like

s = raw_input('Enter a name: ')
    for x in range(0, len(s)):
    print s[x]

There's no need to initialize x, or declare our math statement in the
code. Thoughts, corrections?
    </pre>
  </blockquote>
  <pre wrap=""><!---->

Hi Eric,


Sure, this works great, and you're right: the 'for' loop is the more
appropriate looping construct here, since we're going along the letters of
the name.


We can even do:

###
s = raw_input('Enter a name: ')
for ch in s:
    print ch
###

and skip the manual indicing altogether.  *grin*


Strings are like Lists --- they are both "sequences" --- and the 'for'
loop can deal with them uniformly.  Matt's question asked to do it with
'while', but in real life code, the 'for' loop is usually easier to
construct because it's less open-ended than 'while'.


Good luck!



--__--__--

Message: 4
Date: Mon, 30 Jun 2003 18:53:37 -0400
From: "Derrick 'dman' Hudson" <a class="moz-txt-link-rfc2396E" href="mailto:dman@dman13.dyndns.org">&lt;dman@dman13.dyndns.org&gt;</a>
To: <a class="moz-txt-link-abbreviated" href="mailto:tutor@python.org">tutor@python.org</a>
Subject: Re: [Tutor] Re: Convert list to literal string.


--vEao7xgI/oilGqZ+
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Sat, Jun 28, 2003 at 10:00:04AM -0500, David wrote:

| I can make a loop and process the info.

Dealing with the list of data is certainly doable.  You still didn't
provide the code that shows how you ended up with a list (how!?
command line arguments are strings until you parse them into something
else) so I can't explain what you are doing wrong.

Abel already reminded you of
    ','.join( ['IBM', 'EK', 'MO', 'UTX'] )

(if this helps you, then I suspect you called .split() on the command
line argument, and that's where your list came from)

[... lots and lots of snip ...]
| --addstocks  IBM,EK,MO,UTX

This is easy.  See sample (but untested) code below :


import sys
import getopt

try:
    opts, args =3D getopt.getopt(sys.argv[1:], "", ["addstocks=3D"])
except getopt.GetoptError:
    # print help information and exit:
    usage()     # Note: you have to define this function!
    sys.exit(2)

stock_labels =3D None
for o, a in opts:
    if o =3D=3D "--addstocks" :
        stock_labels =3D a

if not stock_labels :
    usage()
    sys.exit(3)

print stock_labels
print type(stock_labels)
print stock_labels.__class__  # use this with python &gt;=3D 2.2
addstock( stock_labels )


-D

--=20
A violent man entices his neighbor
and leads him down a path that is not good.
        Proverbs 16:29
=20
<a class="moz-txt-link-freetext" href="http://dman13.dyndns.org/~dman/">http://dman13.dyndns.org/~dman/</a>

--vEao7xgI/oilGqZ+
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see <a class="moz-txt-link-freetext" href="http://www.gnupg.org">http://www.gnupg.org</a>

iEYEARECAAYFAj8Av3EACgkQiB6vp1xAVUAiuwCeNkrIAAt2rnOS68eJ7w4Y92h2
MIoAnjnLumBlZlLlNUqGY6BCZeprMnBu
=7tgh
-----END PGP SIGNATURE-----

--vEao7xgI/oilGqZ+--


--__--__--

Message: 5
Date: Mon, 30 Jun 2003 16:04:14 -0700
From: Matthew Richardson <a class="moz-txt-link-rfc2396E" href="mailto:marichar@csusb.edu">&lt;marichar@csusb.edu&gt;</a>
Subject: Re: [Tutor] newbie 'while' question
To: Danny Yoo <a class="moz-txt-link-rfc2396E" href="mailto:dyoo@hkn.eecs.berkeley.edu">&lt;dyoo@hkn.eecs.berkeley.edu&gt;</a>
Cc: Eric L Howard <a class="moz-txt-link-rfc2396E" href="mailto:elh@outreachnetworks.com">&lt;elh@outreachnetworks.com&gt;</a>, Python Tutor <a class="moz-txt-link-rfc2396E" href="mailto:tutor@python.org">&lt;tutor@python.org&gt;</a>
Organization:

On Mon, 2003-06-30 at 15:53, Danny Yoo wrote:
  </pre>
  <blockquote type="cite">
    <blockquote type="cite">
      <blockquote type="cite">
        <pre wrap="">This is very close to the correct solution.  You get the number of
characters in the string with the len() built-in function.  But

  s = raw_input('Enter a name: ')
  x = 0
  while x &lt;= len(s):
      print s[x]
      x += 1

will still result in an IndexError (even though it's close to being
correct).  Do you see why?
        </pre>
      </blockquote>
      <pre wrap="">[newbie swing ;-)]

I know it works, but would it be bad to do something like

s = raw_input('Enter a name: ')
    for x in range(0, len(s)):
    print s[x]

There's no need to initialize x, or declare our math statement in the
code. Thoughts, corrections?
      </pre>
    </blockquote>
    <pre wrap="">
Hi Eric,


Sure, this works great, and you're right: the 'for' loop is the more
appropriate looping construct here, since we're going along the letters of
the name.


We can even do:

###
s = raw_input('Enter a name: ')
for ch in s:
    print ch
###

and skip the manual indicing altogether.  *grin*


Strings are like Lists --- they are both "sequences" --- and the 'for'
loop can deal with them uniformly.  Matt's question asked to do it with
'while', but in real life code, the 'for' loop is usually easier to
construct because it's less open-ended than 'while'.


Good luck!

    </pre>
  </blockquote>
  <pre wrap=""><!---->
The exercise is out of Wesley Chun's 'Core Python Programming.'  The for
loop was far easier for me, but the exercise was to perform the same
task both ways.  No point in skipping over the 'hard' material in the
name of expediency.  I want to get a firm grip on this and ploughing
through all of the examples is the only way I'll get it.

Now for Zak's string manipulation....

Matt

  </pre>
</blockquote>
<br>
</body>
</html>

--------------020509010608030901030803--