2

我是 Python 新手,很难将输出打印在一行上。

这与在线 Python 课程 Learning Python Essentials Lab 5.1.10.6 和打印到 7 段设备有关。如果您不熟悉 7 段设备,请参阅Wikipedia

我没有使用任何外部设备。我只需要它打印到我自己的终端。我发现的所有其他 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 输出 有效,但垂直打印 nums 而不是并排打印;返回=无

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

选项 2 输出 返回有效,但仍然是垂直的并且没有间隔。


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

选项 3 输出 'display' row1 偏移量;根据需要隔开,但垂直;返回=无

  # # # 
     #  
 # # #  
 #      
 # # #  
 # # #  
     #  
 # # #  
     #  
 # # #  
None    
4

1 回答 1

2

您的问题是您在下一个之前打印每个数字,但您需要在下一个之前打印每一。作为一个简化的例子:

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))
于 2020-05-15T00:41:11.133 回答