Home > Net >  How do I print these lines horizontally
How do I print these lines horizontally

Time:08-05

I'm trying to print these numbers (in the form of a digital clock) all on the same line. My goal is to have a function take in a few numbers and print them all on the same line.


numbers = {
0: 
[
    
' _ ',
'| |',
'|_|'

],
1: 
[
    
'   ',
'| |',
'| |'
]
}


for k, v in numbers.items():
    for i in range(len(numbers) 1):
        print(v[i])

This will print a number, for example, but the next one is on the next line down.

If I add end="" at the end of the print statement, the number doesn't appear properly. Ty

CodePudding user response:

numbers = {
    0:
        [
            ' _ ',
            '| |',
            '|_|'
        ],
    1:
        [
            ' /|',
            '  |',
            '  |'
        ],
    2:
        [
            '!--',
            ' / ',
            '|__ '
        ]
}

lines = [''] * 3
for k, v in numbers.items():
    for i in range(len(numbers)):
        lines[i]  = v[i]   '   '

for l in lines:
    print(l)

Prints:

 _     /|   !--   
| |     |    /    
|_|     |   |__    

CodePudding user response:

I propose a slightly different approach to this task. First create a whole line of text to be printed (consisting of a concatenation of the individual elements of all the numbers), and then print it.

for i in range(3):
    output = ""
    for k, v in numbers.items():
        output  = v[i]
    print(output)

CodePudding user response:

Assuming you are using Python3, you can use

for k, v in numbers.items():
   for i in range(len(numbers) 1):
      print(v[i], end = "")

to print inline. You can additional add a symbol inside the end string to separate items.

  • Related