3

I have written a code to display the seven segment output. Whereas I need to display the output side by side. Say input: 123 , output should display seven segment side by side as below

#  ### ###   
#    #   #
#  ### ###
#  #     #
#  ### ###

Here is my logic:

dict = {0:('###','# #','# #','# #','###'),
        1:('  #','  #','  #','  #','  #'),
        2:('###','  #','###','#  ','###'),
        3:('###','  #','###','  #','###'),
        4:('#  ','#  ','###','  #','  #'),
        5:('###','#  ','###','  #','###'),
        6:('###','#  ','###','#  ','###'),
        7:('###','  #','  #','  #','  #'),
        8:('###','# #','###','# #','###')}
value = input("enter value:")
for i in value:
    list1 = dict.get(int(i))
    print('\n'.join(list1))

3 Answers3

3

Try the following:

dict = {0:('###','# #','# #','# #','###'),
        1:('  #','  #','  #','  #','  #'),
        2:('###','  #','###','#  ','###'),
        3:('###','  #','###','  #','###'),
        4:('#  ','#  ','###','  #','  #'),
        5:('###','#  ','###','  #','###'),
        6:('###','#  ','###','#  ','###'),
        7:('###','  #','  #','  #','  #'),
        8:('###','# #','###','# #','###')}
value = input("enter value:")
for seg in range(5):
    print(' '.join([dict.get(int(i))[seg] for i in value]))

You need to print the segments side by side, so you should start by printing the top column of all the numbers, the second, and so on. I'm using a list comprehension to join the numbers together for each column.

AlexApps99
  • 3,506
  • 9
  • 21
0

I think this is the shortest solution...


pattern = ['  # ', '#   ', '# # ', '### ']

numb = [[3, 2, 2, 2, 3], [0, 0, 0, 0, 0], [3, 0, 3, 1, 3], [3, 0, 3, 0, 3], [2, 2, 3, 0, 0], 
       [3, 1, 3, 0, 3], [3, 1, 3, 2, 3], [3, 0, 0, 0, 0], [3, 2, 3, 2, 3], [3, 2, 3, 0, 3]]

nimp = input('Input number: ')

for i in range (5):
    print('')
    for n in nimp:
        print(pattern[numb[int(n)][i]], end=' ')

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

def led_lights(number):
    for i in range(5):
        line =[]
        for j in str(number):
            line.append(numbers[j][i])
        print('  '.join(line))
            
 
led_lights(9081726354)
jmoerdyk
  • 5,544
  • 7
  • 38
  • 49
  • 1
    Is this an answer? If so, where is your explanation of the problem that you are trying to solve or how this answer solves the problem? Please review the [answers] link. – Hovercraft Full Of Eels Feb 23 '23 at 21:55
  • While this code may solve the question, [including an explanation](https://meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply – jmoerdyk Feb 23 '23 at 22:08
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 28 '23 at 09:14