Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to print the elements of a tuple sided by side

I am able to print the elements one after the another but I want the result to be printed side by side: 1234, instead of:
1
2
3
4

Display which I am getting

enter image description here

Desired Output:

enter image description here

The code is to display 7 segment led display.

led_dict={
    '0':('###','# #','# #','# #','###'),
    '1':('  #','  #','  #','  #','  #'),
    '2':('###','  #','###','#  ','###'),
    '3':('###','  #','###','  #','###'),
    '4':('# #','# #','###','  #','  #'),
    '5':('###','#  ','###','  #','###'),
    '6':('###','#  ','###','# #','###'),
    '7':('###','  #','  #','  #','  #'),
    '8':('###','# #','###','# #','###'),
    '9':('###','# #','###','  #','###')
    }

x = input('Enter a number to be displayed: ')

d_list=[]

for num in str(x):
    d_list.append(led_dict[num])
print(d_list)
# for i in range(5):
#     for ab in d_list:
#         print(ab[i])

for ab in d_list:
    for i in range(5):
        print((ab[i]))

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try this:

for i in range(5):
    print(' '.join(k[i] for k in d_list))

How it works;

Each number tuple consists of five lines.
But we have to print by line. That means we have to join the same elements of the tuple for all digits, and then print the resulting string. If we do that five times, (see the index i) we will have printed the whole number.

An example:

Python 3.9.9 (main, Dec 11 2021, 14:34:11) 
>>> led_dict={'0': ('###', '# #', '# #', '# #', '###'),
...  '1': ('  #', '  #', '  #', '  #', '  #'),
...  '2': ('###', '  #', '###', '#  ', '###'),
...  '3': ('###', '  #', '###', '  #', '###'),
...  '4': ('# #', '# #', '###', '  #', '  #'),
...  '5': ('###', '#  ', '###', '  #', '###'),
...  '6': ('###', '#  ', '###', '# #', '###'),
...  '7': ('###', '  #', '  #', '  #', '  #'),
...  '8': ('###', '# #', '###', '# #', '###'),
...  '9': ('###', '# #', '###', '  #', '###')}
>>> d_list=[]
>>> for num in '3427':
...     d_list.append(led_dict[num])
... 
>>> for i in range(5):
...     print(' '.join(k[i] for k in d_list))
... 
### # # ### ###
  # # #   #   #
### ### ###   #
  #   # #     #
###   # ###   #

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading