python - Get output numbers to line up in columns? -


i'm making little program display primes. code (below) produces correct answer, i'm having trouble getting output "nice".

import math n = int(input('list primes to: ')) maxdigits = math.ceil(math.log10(n)) #equal spacing, lesser digit stuff in range(3,n,2):     d in range(2,int(math.sqrt(i))):         if i%d==0:             break     else :         print(str(i),end=' '*(maxdigits-len(str(i)))) 

the idea have many columns possible fit onto output screen, based on maximum number of digits prime have. right when run program displays numbers in long line, without newlines. results output looking this:

it's ugly! burns eyes! http://img194.imageshack.us/img194/5522/2eyd.png

which...isn't going for. if weren't fact numbers wrap on lines, , knew go next line if there isn't enough space, work fine. how can rid of annoying wrapping in program? more elegant (i.e. please don't tell me have count terminal width , divide maxdigits) better...:d

you can add line breaks whenever wish. simple approach add '\n' character after number of numbers, every 10. more complex approach determine width of terminal , calculate how many numbers can output per line.

you might want check out str.format() allows set width of each column in table. check out format string syntax details options available formatting.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -