Question

I want to write a program in python that represents the factors of numbers graphically. I don't know how to explain exactly what I mean with words, so I will describe the output I am looking for:

* - - - - - - - - - - - - - - - - - - -
* * - - - - - - - - - - - - - - - - - -
* - * - - - - - - - - - - - - - - - - -
* * - * - - - - - - - - - - - - - - - -
* - - - * - - - - - - - - - - - - - - -
* * * - - * - - - - - - - - - - - - - -
* - - - - - * - - - - - - - - - - - - -
* * - * - - - * - - - - - - - - - - - -
* - * - - - - - * - - - - - - - - - - -
* * - - * - - - - * - - - - - - - - - -
* - - - - - - - - - * - - - - - - - - -
* * * * - * - - - - - * - - - - - - - -
* - - - - - - - - - - - * - - - - - - -
* * - - - - * - - - - - - * - - - - - -
* - * - * - - - - - - - - - * - - - - -
* * - * - - - * - - - - - - - * - - - -
* - - - - - - - - - - - - - - - * - - -
* * * - - * - - * - - - - - - - - * - -
* - - - - - - - - - - - - - - - - - * -
* * - * * - - - - * - - - - - - - - - *

I wrote this program:

for i in range (1,21):
    for j in range (1,21):
        if i%j == 0:
            print '*',
        else:
            print '_',

But the output is on a single line and I don't know how to fix that. I played around with the commas but I still don't get what I want.

By the way, almost all of my questions are about problems with formatting the output. How can I improve at doing that? There is no chapter in a book or tutorial dedicated to formatting the output so I don't know how to improve.

Was it helpful?

Solution

Just add a print statement in your first for loop:

for i in range (1,21):
    for j in range (1,21):
        if i%j == 0:
            print '*',
        else:
            print '-',
    print

[OUTPUT]
* - - - - - - - - - - - - - - - - - - -
* * - - - - - - - - - - - - - - - - - -
* - * - - - - - - - - - - - - - - - - -
* * - * - - - - - - - - - - - - - - - -
* - - - * - - - - - - - - - - - - - - -
* * * - - * - - - - - - - - - - - - - -
* - - - - - * - - - - - - - - - - - - -
* * - * - - - * - - - - - - - - - - - -
* - * - - - - - * - - - - - - - - - - -
* * - - * - - - - * - - - - - - - - - -
* - - - - - - - - - * - - - - - - - - -
* * * * - * - - - - - * - - - - - - - -
* - - - - - - - - - - - * - - - - - - -
* * - - - - * - - - - - - * - - - - - -
* - * - * - - - - - - - - - * - - - - -
* * - * - - - * - - - - - - - * - - - -
* - - - - - - - - - - - - - - - * - - -
* * * - - * - - * - - - - - - - - * - -
* - - - - - - - - - - - - - - - - - * -
* * - * * - - - - * - - - - - - - - - *

ALTERNATIVE

b = [' '.join('*' if i==j or i%j==0 else '-' for j in range(1,21)) for i in range(1,21)]

for i in b:
    print i

[SAME OUTPUT]

Tom Fenech's suggestion

You could do it in one line as:

print '\n'.join(' '.join('*' if i % j == 0 else '_' for j in xrange(1,21)) for i in xrange(1,21))

OTHER TIPS

just include a print statement after every iteration of your first for loop ... or in other words I mean that you have to print a line break every time , the i is incremented in your case and in python you can print a line break by using following syntax :

print 

or

print "\n"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top