I wrote a program to list all the prime numbers less than a number the user inputs. When I run the program in PyCharm, the output is very nice: each number is properly situated on the line and no numbers are spread across two lines. However, when I run the program from the command line, the formatting gets weird, with numbers at the end of a row sometimes getting cut off.
Here is my code:
import prime_functions as pf
number = int(input("Find primes up to what number? "))
# Save primes to a list.
primes = pf.list_primes(number)
for prime in primes[0:len(primes)-1]:
print(prime, end=', ')
print(primes[len(primes)-1])
# Print length of primes.
print(f'Number of primes less than {number}: {len(primes)}')
# Pause before exiting.
input()
The list_primes
function simply checks whether each odd number from three to the user's number is prime and returns a list of all the primes it finds.
What I would like to do ideally is print a small slice of the primes
list on each line (say, five elements per line), but I can't think of how to do that without abandoning the generality of the program and using a bunch of for-loops. Are there any Python tricks out there that will help me?