Python3 Print on Same Line - Numbers in 7-Segment-Device Format

后端 未结 1 1816
离开以前
离开以前 2021-01-24 06:28

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

1条回答
  •  被撕碎了的回忆
    2021-01-24 06:44

    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))
    

    0 讨论(0)
提交回复
热议问题