I am new to Python and am having trouble printing the output on one line.
This relates to the Python Learning Python Essentials Lab 5.1.10.6 online class and printing to a 7-segment device. If you're not familiar with a 7-segment device, check out Wikipedia.
I do not use any external device. I only need it to print on my own terminal. All the other StackOverflow solutions that I have found are related to the use of real devices and have not helped.
-
Laboratory link:
https://edube.org/learn/programming-essentials-in-python-part-2/lab-a-led-display
-
Purpose: To request a number from the user; print number on 7-segment display
format to your terminal.
- Notes: Usage Python 3.9. I have tried 3 alternative solutions (Option 1,2,3), but none do what I want.
- INSTRUCTIONS: One / How Option 1, 2 or 3 to execute only this option
- I found this alternative solution, which I mainly understand and which achieves what I am trying to accomplish. However, this is a totally different approach and not the one I would have imagined. I know there are many ways to dress up a 7-segment device, and if that is the most correct, then I will learn it. But I feel like I'm so close and only superfluous
'n'
far from finding out with my own method and i'm just trying to figure out 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':('###',"https://serverfault.com/#",'###',"https://serverfault.com/#",'###'),
'3':('###',"https://serverfault.com/#",'###',"https://serverfault.com/#",'###'),
'4':('# #','# #','###',"https://serverfault.com/#","https://serverfault.com/#"),
'5':('###',"https://serverfault.com/#",'###',"https://serverfault.com/#",'###'),
'6':('###',"https://serverfault.com/#",'###','# #','###'),
'7':('###',"https://serverfault.com/#","https://serverfault.com/#","https://serverfault.com/#","https://serverfault.com/#"),
'8':('###','# #','###','# #','###'),
'9':('###','# #','###',"https://serverfault.com/#",'###')
}
# 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"https://serverfault.com/#"''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 Exit
Works, but prints nums vertically instead of side by side; Return = None
# # #
#
# # #
#
# # #
# # #
#
# # #
#
# # #
None
Option 2 Exit
Return works, but always vertical and not spaced
###
#
###
#
###
###
#
###
#
###
Option 3 Exit
& # 39; display & # 39; offset row1; spaced as desired, but vertical; Return = None
# # #
#
# # #
#
# # #
# # #
#
# # #
#
# # #
None