[Tutor] List all possible 10digit number

Dave Angel d at davea.name
Sat Sep 1 07:00:10 CEST 2012


On 08/31/2012 10:14 PM, Scurvy Scott wrote:
> Thanks for the reply. This isn't an assignment per se as I'm just learning python for my own sake- not in classes or school or what have you. As I said I'm pretty new to python picking up whatever I can and generators are something that I haven't grasped. They're functions(kinda) that in some way deal with lists, tuples, or dict,(kinda?). I am probably wrong on this. Some clarification would be excellent. 
> 
> Thank you in advance and I apologize for being not so sharp sometimes.
> 
> 

You seem plenty sharp enough to me.  I do have to point out a couple of
list etiquette points:

1) You top-posted.  That means you put your message BEFORE the part
you're quoting.  Once a few replies go back and forth, this thoroughly
scrambles the order, so you might as well delete all the context.

2) You replied privately to me.  I've been participating in public
forums like this for over 20 years, and the key word is "public." The
only messages to send privately are thank-yous, and ones with personal
information in them, such as passwords and such.  Instead you should
reply-all, and if your email is too broken to offer that, add the cc of
tutor at python.org

A simple generator could be

def bigrange(start, end):
    i = start
    while i < end:
        yield i
        i += 1

The yield is a little like a return, in that the "caller" gets the
value.  But the function stack is kept active, and next time the loop
needs a value, it resumes the same function.  Control gets batted back
and forth between the loop code and the generator code, until the
generator finally returns.  In this case it returns when it reaches the
end value.

So it'd be used like:

for x in bigrange(100, 1000):
    print x

I do hope you've been testing with smaller numbers than 10**10, to make
sure the loops you write really do start and end with reasonable results.


> I = 1000000000
> While I < 9999999999:
>     Print I

> Is that more like it?
-- 

Did you try running it?  You never increment I, so it'll repeat forever
on the one value.

> I meant;
>
> I = 1000000000
> While I < 9999999999:
>     I += 1
>     Print I
>
>
> But that doesn't work.

Want to explain what about it doesn't work?  That phrase could mean that
you got an error (post traceback), or it ran forever, or it displayed
roman numerals.

  The only problem I see is it starts one-too-high.  Fix that by
swapping the last two lines.

> Apologies for not mentioning I'm on 2.x.x I've seen so much about
> avoiding 3.x I just thought it was assumed.

And I've seen so much about avoiding 2.x that i figured 3.x would be
assumed.  Best to be explicit:   python version, operating system, etc.


DaveA


More information about the Tutor mailing list