Python Idiom Question

Steve Holden sholden at holdenweb.com
Tue Jul 10 02:53:28 CEST 2001


"Steve Holden" <sholden at holdenweb.com> wrote in message
news:8Zr27.2394$vA6.115437 at e420r-atl1.usenetserver.com...
> "Tim Daneliuk" <tundra at tundraware.com> wrote in ...
> > What is the equivalent python idiom to the following 'C' code:
> >
> > while (x=getchar() != 'N')
> > {
> > Do something}
> >
> > In other words, is there a convenient way to simultaneously get
> > input, from say, raw_input() or f.read(), and check the return
> > value agains some logical construct, re pattern match, or whatever?
> >
> > I'm sure this is possible, I just cannot seem to find the magic
> > combination that gets me there.  Nothing seems to turn up when I
> > look in the Usual Places, but then again, I may not be Lookin' Right.
> >
> > At the moment, I'm doing things like:
> >
> > x=raw_input()
> > while x:
> > Do Something Interesting
> > x = get_input("Prompt: ")
> >
> The approved idiom is
>
> x = raw_input()
> while 1:
>     if not x:
>         break
>     DoSomethingInteresting()
>
while 1:
    x = raw_input()
    if not x:
        break
    DoSomethingInteresting()

Most people do prefer the loop to iterate more than once... sori!







More information about the Python-list mailing list