Python3 在同一行打印 - 7 段设备格式的数字 [英] Python3 Print on Same Line - Numbers in 7-Segment-Device Format
问题描述
我是 Python 新手,无法将输出打印在一行上.
这是关于在线 Python 课程 Learning Python Essentials Lab 5.1.10.6 和打印到 7 段设备.如果您不熟悉 7 段设备,请参阅 维基百科.
我没有使用任何外部设备.我只需要它打印到我自己的终端.我发现的所有其他 StackOverflow 解决方案都与使用实际设备有关,并没有帮助.
实验室链接:https://edube.org/学习/编程-essentials-in-python-part-2/lab-a-led-display
目的:提示用户输入号码;7段显示打印号码格式到您的终端.
- 注意事项:使用 Python3.9.我尝试了 3 种替代解决方案(选项 1、2、3),但没有一个按照我的意愿行事.
- 说明:取消/注释选项 1、2 或 3 以仅运行该选项
- 我确实找到了这个替代解决方案,我基本上理解了.然而,这是一种完全不同的方法,而不是我想出的方法.我知道有很多方法可以为 7 段设备蒙皮,如果这是最正确的,那么我会学习它.但我觉得我离用我自己的方法弄清楚它并试图理解我错过了什么只有一个多余的
'\n'
.
感谢您的帮助.
期望输出
### ## ### ### # # ### ### ### ### #################################################################################################################################################################################################################################################################################################################################################
我的代码
# 每次运行脚本时清屏导入操作系统clear = lambda: os.system('cls')清除()## (number:7-segment-hash) 的字典dict1 = {'0':('###','##','##','##','###'),'1':('#####'),'2':('###','#','###','#','###'),'3':('###','#','###','#','###'),'4':('##','##','###','#','#'),'5':('###','#','###','#','###'),'6':('###','#','###','##','###'),'7':('###','#','#','#','#'),'8':('###','##','###','##','###'),'9':('###','##','###','#','###')}# 以 7 段设备格式打印数字的函数def fun_PrintNums(num):如果数量<0 或 num % 1 >0 或 type(num)!=int: # 如果 num 不是正整数返回无效输入,请重试"别的:显示 = [' ']for i in str(num): # 将 'num' 转换为 STRING;对于字符串 'num' 中的每个数字"#'''选项 1:有效,但垂直打印 nums,而不是并排打印;返回=无'''#对于 dict1[i] 中的字符:打印(*字符)打印(fun_PrintNums(int(输入(输入任何整数字符串:))))#----------------------------------------------------------------##''' 选项 2: Return 有效,但仍然垂直且没有间隔 ''' ## 对于 dict1[i] 中的字符:# display.append(char)# 返回显示# print('\n'.join(fun_PrintNums(int(input("请输入任意整数字符串:")))))#---------------------------------------------------------------------##''' 选项 3: 'display' row1 偏移量;根据需要间隔开,但垂直;返回=无''' ## 对于 dict1[i] 中的字符:# 显示 += 字符# 显示 += '\n'# a = print(*display,end='')# 返回一个# print(fun_PrintNums(int(input("请输入任意整数字符串:"))))#---------------------------------------------------------------#
选项 1 输出有效,但垂直打印 nums 而不是并排打印;返回=无
# # ####################没有任何
选项 2 输出Return 有效,但仍然垂直且没有间隔.
<预><代码>######################选项 3 输出'显示' row1 偏移量;根据需要间隔开,但垂直;返回=无
# # ####################没有任何
你的问题是你在下一个之前打印每个数字,但你需要在下一个之前打印每一行.举个简单的例子:
dict1 = {'0':('###','##','##','##','###'),'1':('##','###','##','##','##'),'2':('###','#','###','#','###'),'3':('###','#','###','#','###'),'4':('##','##','###','#','#'),'5':('###','#','###','#','###'),'6':('###','#','###','##','###'),'7':('###','#','#','#','#'),'8':('###','##','###','##','###'),'9':('###','##','###','#','###')}数量 = '0123456789'对于范围内的行(len(dict1['0'])):print(' '.join(dict1[i][row] for i in num))
输出:
### ## ### ### # # ### ### ### ### #################################################################################################################################################################################################################################################################################################################################################
如果你不想在 join
中使用列表推导式,你可以像这样展开:
for row in range(len(dict1['0'])):行 = []对于 num:line.append(dict1[i][row])打印(' '.join(行))
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
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))
这篇关于Python3 在同一行打印 - 7 段设备格式的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!