Search code examples
pythonpython-3.xprintingseven-segment-display

Python3 Print on Same Line - Numbers in 7-Segment-Device Format


I'm new to Python and having difficulty getting the output to print on one line.

This is pertaining to the online Python class Learning Python Essentials Lab 5.1.10.6 and printing to a 7-segment-device. If you are unfamiliar with a 7-segment-device, see Wikipedia.

I am NOT using any external device. I only need it to print to my own terminal. All the other StackOverflow solutions I found are related to using actual devices and didn't help.

  • Lab Link: https://edube.org/learn/programming-essentials-in-python-part-2/lab-a-led-display

  • Purpose: Prompt user for number; print number in 7-segment display format to your terminal.

  • Notes: Using Python3.9. I tried 3 alternate solutions (Option 1,2,3), but none do what I want it to.
  • INSTRUCTIONS: Un/Comment Option 1,2,or 3 to run just that option
  • I did find this alternate solution, which I mostly understand. However, it's a totally different approach and not one I would have come up with. I know there are many ways to skin a 7-segment-device, and if this is the most correct, then I'll learn it. But I feel like I'm so close and only a superfluous '\n' away from figuring it out with my own method and trying to understand what I'm missing.

Thank you for your help.

Desired Output

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

My Code

# clear screen each time you run the script
import os
clear = lambda: os.system('cls')
clear()
# 
# Dictionary of (number:7-segment-hash)
dict1 = {
    '0':('###','# #','# #','# #','###'),
    '1':('#####'),
    '2':('###','  #','###','#  ','###'),
    '3':('###','  #','###','  #','###'),
    '4':('# #','# #','###','  #','  #'),
    '5':('###','#  ','###','  #','###'),
    '6':('###','#  ','###','# #','###'),
    '7':('###','  #','  #','  #','  #'),
    '8':('###','# #','###','# #','###'),
    '9':('###','# #','###','  #','###')
}

# Function to print numbers in 7-segment-device format
def fun_PrintNums(num):
    if num < 0 or num % 1 > 0 or type(num)!=int:    # if num is NOT a positive whole integer
        return "Invalid entry, please try again"
    else: 
        display = [' ']
        for i in str(num):      # convert 'num' to STRING; for each "number" in string 'num'


#'''Option 1: works, but prints nums vertically instead of side-by-side; Return=None ''' #
            for char in dict1[i]:
                print(*char)
print(fun_PrintNums(int(input("Enter any string of whole numbers: "))))
#----------------------------------------------------------------#


#''' Option 2: Return works, but still vertical and not spaced out ''' #
#             for char in dict1[i]:
#                 display.append(char)
#     return display
# print('\n'.join(fun_PrintNums(int(input("Enter any string of whole numbers: ")))))
#---------------------------------------------------------------------#

#''' Option 3: 'display' row1 offset; spaced out as desired, but vertical; Return=None''' #
#             for char in dict1[i]:                
#                 display += char
#                 display += '\n'
#     a = print(*display,end='')
#     return a
# print(fun_PrintNums(int(input("Enter any string of whole numbers: "))))
#---------------------------------------------------------------#

Option 1 Output Works, but prints nums vertically instead of side-by-side; Return=None

# # #
    #
# # #
#    
# # #
# # #
    #
# # #
    #
# # #
None 

Option 2 Output Return works, but still vertical and not spaced out.


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

Option 3 Output 'display' row1 offset; spaced out as desired, but vertical; Return=None

  # # # 
     #  
 # # #  
 #      
 # # #  
 # # #  
     #  
 # # #  
     #  
 # # #  
None    

Solution

  • Your problem is that you are printing each number before the next, but you need to print each row before the next. As a simplified example:

    dict1 = {
        '0':('###','# #','# #','# #','###'),
        '1':(' ##','###',' ##',' ##',' ##'),
        '2':('###','  #','###','#  ','###'),
        '3':('###','  #','###','  #','###'),
        '4':('# #','# #','###','  #','  #'),
        '5':('###','#  ','###','  #','###'),
        '6':('###','#  ','###','# #','###'),
        '7':('###','  #','  #','  #','  #'),
        '8':('###','# #','###','# #','###'),
        '9':('###','# #','###','  #','###')
    }
    
    num = '0123456789'
    
    for row in range(len(dict1['0'])):
        print(' '.join(dict1[i][row] for i in num))
    

    Output:

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

    If you don't want to use a list comprehension inside join, you can unroll that like this:

    for row in range(len(dict1['0'])):
        line = []
        for i in num:
            line.append(dict1[i][row])
        print(' '.join(line))