ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> python-Tutor
python-Tutor
Re: [Tutor] newbie 'while' question
by Matthew Richardson other posts by this author
Jun 30 2003 11:04PM messages near this date
Re: [Tutor] newbie 'while' question | Re: [Tutor] newbie 'while' question
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

-- 
Matt Richardson
Instructional Support Technician
Department of Art
CSU San Bernardino



_______________________________________________
Tutor maillist  -  Tutor@[...].org
http://mail.python.org/mailman/listinfo/tutor
Thread:
Matthew Richardson
Danny Yoo
Matthew Richardson
Kalle Svensson
Eric L Howard
Danny Yoo
Matthew Richardson
Matthew Richardson
Matthew Richardson
Danny Yoo
Matthew Richardson
Danny Yoo
Zak Arntson
Zak Arntson
Zak Arntson

Zak Arntson

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved