### Thread: list index out of range

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2017
Posts
2
Rep Power
0

#### list index out of range

I'm writing a program to find all of the prime numbers in a given range, and display them evenly through rows and columns, but I recently am stuck needing literally one if-statement and for some reason, I'm just blanking out.

Right before the "print(primes[row + 56 * col], "\t", end = "")" line, I need an if-statement. There are 556 elements in the list, and my table is 56 rows * 10 columns, which is 560. I need a way to just print out the last column even if the last lines in it have blank rows.

Code:
```def findPrimes(n):
""" Return a list of prime numbers between 1 to n. """
prime_list = [2]

for number in range(3, n, 2):
if all(number % i != 0 for i in range(2, int(number ** .5) + 1)):
prime_list.append(number)

return prime_list

def displayTable(n):
""" Makes a table of rows and columns to display a list. """
primes = findPrimes(n)
rows = 56
columns = 10

for row in range(rows):
for col in range(columns):
print(primes[row + 56 * col], "\t", end = "")
print()

displayTable(4027)

# statistics
print("\nThe number of primes in the given range is: " + str(len(findPrimes(4027))))```
2. Your table is missing these primes, so the problems run deeply. Computed by Ken Iverson's final dialect of APL
Code:
```251 601 983 1399 1789 2243 2687 3137 3583
257 607 991 1409 1801 2251 2689 3163 3593
263 613 997 1423 1811 2267 2693 3167 3607```
Otherwise, the number you've listed are prime.
3. Before fixing displayTable you need a new division of labor. The arguments should be a list and some other column formatting information. As you have it, displayTable can only display primes in 10 rows by 56 columns.
Code:
```#! /usr/bin/python3

'''
command `python3 -m doctest thisfile.py' to run the doctests
'''

import sys

def findPrimes(n):
''' Return a list of prime numbers between 1 to n.

>>> findPrimes(5)
[2, 3]
'''
return [2] + [number for number in range(3, n, 2) if all(number % i for i in range(2, int(number ** .5) + 1))]

def displayTable(L:list, columns: int, field_format: callable, sink: open = sys.stdout) -> None:
'''formats items of L in "phone book order" and writes them to the sink

>>> import io
>>> with io.StringIO() as sink:
...   	displayTable('abc', 2, "{:1s}".format, sink)
...   	junk = sink.seek(0) # hide the output
...
ac
b
>>>
'''
skip = len(L) / columns  # python3 required
skip = int(skip) + (int(skip) != skip)

sink.write('\n'.join(''.join(field_format(L[i]) for i in range(row, len(L), skip))for row in range(skip))+'\n')

def main(n = 4027):
primes = findPrimes(n)
displayTable(primes, columns = 10, field_format = "{:7d}")

# statistics
print("\nThe number of primes in the given range is: {}".format(len(primes)))```
I hope you find this a more satisfactory solution and recognize that this version needs to find the prime numbers just one time.
Last edited by b49P23TIvg; September 22nd, 2017 at 11:00 PM. Reason: fixed.