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 Danny Yoo other posts by this author
Jun 30 2003 9:20PM messages near this date
Re: [Tutor] newbie 'while' question | Re: [Tutor] newbie 'while' question
>  Here's what I've got for the second scenario:
> 
>  s = raw_input('Enter a name: ')
>  x = 0
>  while x >= 0:
>      print s[x]
>      x += 1
> 
>  The error is expected because the counter goes on beyond the number of
>  characters in the string and runs out of stuff to print.


Hi Matthew,


Ok, so that means that we want:


###
while some_expression_that_is_false_when_x_is_too_large:
    print s[x]
    x += 1
###



The expression that we have, right now:

    x > = 0

is true as long as x is either positive or zero.  ("nonnegative").  The
only problem is that this will always be true: nothing in the loop's body
ever causes x to become negative.  *grin*


'x' is always increasing, hence our program breaks when it tries to print
nonexistant characters.  So we need to choose a different condition,
something that is true only until the index reaches the end of the word.


That is, instead of


        "Matthew Richardson"
        (------------------------------------------> 
        0                                        infinity


We want to bound x's range to just:


        "Matthew Richardson"
        (------------------)-----------------------> 
        0                                        infinity



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!


_______________________________________________
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