[Tutor] newbie 'while' question

tpc@csua.berkeley.edu tpc@csua.berkeley.edu
Mon Jun 30 15:14:01 2003


I just tried doing this and was successful.

response = raw_input('Please type your first name: ')
length = len(response)
while length > 0:
    print response[-length]
    length = length - 1



On Mon, 30 Jun 2003, Matthew Richardson wrote:

> On Mon, 2003-06-30 at 11:11, Danny Yoo wrote:
> > On Mon, 30 Jun 2003, Matthew Richardson wrote:
> >
> > > s = raw_input('Enter a name: ')
> > > for x in s:
> > >     print x
> > >
> > > Trying to do it with 'while', I've only managed to get it to print
> > > through to the last character, where it loops and continues to print the
> > > last character until escaped.  Someone willing to point me in the right
> > > direction?
> >
> >
> >
> > Hi Matt,
> >
> >
> > Sure, but you need to show us your while loop first.
> >
> >
> > It sounds like you've almost got it right, so I suspect it has to do
> > something with the while loop condition always being being a true value.
> > But we really do need to see the code so that we don't have to read your
> > mind.  *grin*
> >
> >
> >
> > Good luck to you!
> >
> Hi,
> Sorry that I omitted my code in the last post.  I've been over it more
> times than I can count and lost track of which *.py did what.  I've
> actually had two results, the first one (already mentioned) of getting a
> loop on the last character and the second where the string gets printed
> and then exits an error when it runs out of characters to print.  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.  Got that part
> of it.  How can I count the number of characters in the string?  Then
> the while statement could be changed to
>
> while x <= NumOfChar:
>     dosomestuff
>
> Or maybe I'm off track here, too.
>
> I should give a proper introduction of myself, I can't remember if I did
> when I joined the list.  My name is Matt, I'm a lab tech at Cal State
> San Bernardino, managing a photo lab and three computer labs (budget
> cuts create strange job descriptions).  Python appeals to me because of
> it's power and syntax.  I looked into learning Perl first, but found it
> to be hard to follow.  No programming experience, no computer sci
> education, just a lot of on the job training in system administration
> and lab management.  Python would be a huge help in automating a lot of
> things done manually in maintaining the labs.
>
> Thanks for the help,
>
> Matt
>
>
>
> --
> Matt Richardson
> Instructional Support Technician
> Department of Art
> CSU San Bernardino
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>