您的地址 发表于 2018-4-8 18:06:24

把数字转换成LCD显示模式


''' Python小练习(001):
把数字(0~9)转换成LCD显示的模式。
例如:0:    1:   2:   3:   4:   5:   6:   7:   8:   9:
         _            _       _            _      _      _      _      _
      ||   |      _|      _|    |_|    |_   |_       |    |_|    |_|
      |_|   |   |_      _|       |      _|    |_       |    |_|   _|

如果输入数字: 1234, 输出:   
                                         _      _      
                                 |      _|   _|    |_|
                                 |   |_      _|      |
'''


思路根据LCD特性,分为3层,可以使用list,dict形式来存储每特性


代码:

lcd={0:[' _','| | ','|_| '],\
   1:['    ','| ','| '],\
   2:[' _',' _| ','|_'],\
   3:[' _',' _| ',' _| '],\
   4:['    ','|_| ','| '],\
   5:[' _','|_',' _| '],\
   6:[' _','|_','|_| '],\
   7:[' _','| ','| '],\
   8:[' _','|_| ','|_| '],\
   9:[' _','|_| ',' _| ']}

def number2lcd(n):
        global lcd
        output = [' ',' ',' ']
        l = len(str(n))
        while True:
                i = n // 10 **(l-1)
                for j in range(3):
                        output += lcd
                n = n%10**(l-1)
                l -=1
                if 1==0:
                        break
        return output
n = int(raw_intput('Input the numbers:'))
output = number2lcd(n)
for each in output:
        print each

来自:jerryxjr1220
页: [1]
查看完整版本: 把数字转换成LCD显示模式