0

Getting a list of __builtins__ in IDLE how do I pause it after a certain number or bottom of the screen?

This gives me one at a time..

>>> for i in dir(__builtins__):
...     print i
...     raw_input("Press Enter...")

and I could slice it like ...

x=dir(__builtins__)
len(x)    
for i in x[:10]:
print i

... and that give me first 10 but is there a way to get it to print 10, or bottom of screen until the user input ? Like a less or more in Unix?

Thanks!

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
JohnRain
  • 41
  • 2
  • 9

1 Answers1

0

Try something like:

print_every = 5

for i, f in enumerate(dir(__builtins__)):
    print f

    if i % print_every == 0 and i != 0:
        raw_input("Press Enter...")
  • enumerate pairs each entry in the list with its index in the list
  • if i % print_every == 0: checks to see if i (the current index) is a multiple of print_every.

The above code should print the list in groups of print_every many entries.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
  • Grazi!! Many thanks especially for the explanation. – JohnRain May 28 '17 at 16:24
  • @JohnRain Note, I actually have the condition wrong. It will only print 1 entry in the first iteration. I'll fix it. Check back in a couple minutes. – Carcigenicate May 28 '17 at 16:25
  • @JohnRain There, it's mostly fix. It will currently print `print_every` + 1 many entries in the first iteration. With some tinkering, I'm sure you'll be able to fix it up. – Carcigenicate May 28 '17 at 16:28
  • works great, perfect for poking around... out of curiousity does it not work in 3.4? – JohnRain May 30 '17 at 00:58
  • @JohnRain You'd need to change `print` to a function, and `raw-input` to `input`, but yes it works in 3. I tested it using QPython3. And if this answer helped you, I'd appreciate an upvote. – Carcigenicate May 30 '17 at 01:03
  • thank you sir! Dude I'd love to give you an upvote but I don't have the 'reputation' yet. It says it's recorded it but doesn't show it??? Also, your answer looks a lot simpler, more pythonic, than the ones that Martjin marked as a duplicate. – JohnRain Jun 04 '17 at 18:07
  • @JohnRain Np, that's ok. And the duplicate answer is actually very similar to mine. Note it also uses `enumerate`. The main differences are that theirs allows early exit by inputting "q", while mine loops until the entire list is printed, and that there's works for different lists while mine only loops over `dir(__builtins__)`. I wanted to keep it simple to act as an approachable example. I'm glad this helped you. – Carcigenicate Jun 04 '17 at 18:15