我是Python的新手,很难让输出打印在一行上。
这与在线 Python 课程学习 Python Essentials Lab 5.1.10.6 和打印到 7 段设备有关。 如果您不熟悉 7 段设备,请参阅维基百科。
我没有使用任何外部设备。我只需要它打印到我自己的终端。 我发现的所有其他StackOverflow解决方案都与使用实际设备有关,并没有帮助。
-
实验室链接: https://edube.org/learn/programming-essentials-in-python-part-2/lab-a-led-display
-
用途:提示用户输入号码;在 7 段显示屏中打印号码 格式化到您的终端。
- 注意:使用Python3.9。 我尝试了 3 种替代解决方案(选项 1,2,3(,但没有一种能做到我想要的。
- 说明:取消/注释选项 1、2 或 3 以仅运行该选项 我
- 确实找到了这个替代解决方案,我基本上理解。 然而,这是一种完全不同的方法,不是我会想出的方法。 我知道有很多方法可以蒙皮 7 段设备,如果这是最正确的,那么我会学习它。 但我觉得我是如此接近,距离用自己的方法弄清楚它并试图理解我错过了什么只有多余的
'n'
。
谢谢你的帮助。
期望的输出
### ## ### ### # # ### ### ### ### ###
# # ### # # # # # # # # # # #
# # ## ### ### ### ### ### # ### ###
# # ## # # # # # # # # # #
### ## ### ### # ### ### # ### ###
我的代码
# 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: "))))
#---------------------------------------------------------------#
选项 1 输出有效,但垂直打印数字而不是并排打印;返回=无
# # #
#
# # #
#
# # #
# # #
#
# # #
#
# # #
None
选项 2 输出返回有效,但仍然是垂直的,没有间隔。
###
#
###
#
###
###
#
###
#
###
选项 3 输出"显示"行 1 偏移量; 根据需要间隔,但垂直;返回=无
# # #
#
# # #
#
# # #
# # #
#
# # #
#
# # #
None
您的问题是您在下一个数字之前打印每个数字,但您需要在下一行之前打印每一行。举个简化的例子:
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))
输出:
### ## ### ### # # ### ### ### ### ###
# # ### # # # # # # # # # # #
# # ## ### ### ### ### ### # ### ###
# # ## # # # # # # # # # #
### ## ### ### # ### ### # ### ###
如果你不想在join
中使用列表推导,你可以像这样展开它:
for row in range(len(dict1['0'])):
line = []
for i in num:
line.append(dict1[i][row])
print(' '.join(line))