gpt4 book ai didi

Python3 在同一行打印 - 7 段设备格式的数字

转载 作者:行者123 更新时间:2023-12-04 10:01:15 26 4
gpt4 key购买 nike

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

这与在线 Python 类(class) 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 以仅运行该选项
  • 我确实找到了 this alternate solution ,我大致理解。然而,这是一种完全不同的方法,而不是我想出的方法。我知道有很多方法可以为 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 输出
    Return 有效,但仍然垂直且没有间隔。

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

    选项 3 输出
    '显示' row1 偏移量;根据需要间隔开,但垂直;返回=无
      # # # 
    #
    # # #
    #
    # # #
    # # #
    #
    # # #
    #
    # # #
    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))

    关于Python3 在同一行打印 - 7 段设备格式的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61809719/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com