0

I wan to write a program that takes a number and prints out a graphical representation of it to the console. For example, with the input 123 I should expect to get:

enter image description here

Here's how I tried to implement it, starting with implementing one and two.

def g (n):
    n = list(str(n))
    dic = { '1': """1   
                  1
                  1
                  1
                  1
                  1
                  1
                  """,
            '2': """2222
                      2
                       2
                       2
                  2  2
                2
                2
                2
                  22222"""
            }
    for i in n:
        print(dic[i])

However I get this for the input '121':

>>> 
>>> g(121)
1   
                  1
                  1
                  1
                  1
                  1
                  1

2222
                      2
                       2
                       2
                  2  2
                2
                2
                2
                  22222
1   
                  1
                  1
                  1
                  1
                  1
                  1

UPDATE: I corrected the syntax error. Thanks. I still get a somewhat weird result.

Morteza R
  • 2,229
  • 4
  • 20
  • 31
  • 2
    You're using a semi-colon in the dictionary. Use `,`. – Alex Riley Dec 22 '14 at 12:52
  • It also won't work, because it will print each digit in a new row. You have to prepare each line of the text before printing. – unddoch Dec 22 '14 at 12:55
  • I corrected the syntax error. I still get a weird result. – Morteza R Dec 22 '14 at 12:58
  • 1
    to print ascii-art, you could use figlet e.g., `print(pyfiglet.figlet_format('1234', font='doh'))`. Another [code example](http://stackoverflow.com/q/9632995/4279). – jfs Dec 23 '14 at 00:02

1 Answers1

2

The """text""" syntax in python means that every character you enter will be included in the string, including white space. Therefor, if you indent the digits in the text the whitespace will be included in the string and will be printed, that's why you see the digits so far off to the right.

That said, your algorithm still won't work because of the way the console works. When printing something, you print each line separately, one after the other. You can't, using the print function, print one part of the first line, part of the second line, and then print second part of the previous line. Once you print a line, you can't go back.

You should make it something like this:

for i in range(NUMBER_OF_LINES):
    s = ""
    for j in n:
        s += dic[j].split('\n')[i]  # There are more efficient ways to do this part...
    print(s)

By the way, if you need to print without new line, you can use it like this: print("Hello World!", end="")

unddoch
  • 5,790
  • 1
  • 24
  • 37
  • Unfortunately I still get the same weird look when I apply your algorithm. In the first row, characters are pushed to the left corner. – Morteza R Dec 22 '14 at 13:16
  • Did you fix the indentation? – unddoch Dec 22 '14 at 13:18
  • 1
    actually, you can go back using e.g., `blessings` (positioning ANSI codes) and `colorama` (Windows support) packages. Though you don't need to to print the digits. – jfs Dec 22 '14 at 23:52