MagicforestA 2022-06-13 14:32 采纳率: 100%
浏览 45
已结题

需要简化代码sevendisplaydevice

def print_number(num):
    num=str(num)
    for i in range(len(num)):
        if num[i]=='0':
            print("### ",end="")
        elif num[i]=='1':
            print("  # ",end="")
        elif num[i]=='2':
            print("### ",end="")
        elif num[i]=='3':
            print("### ",end="")
        elif num[i]=='4':
            print("# # ",end="")
        elif num[i]=='5':
            print("### ",end="")
        elif num[i]=='6':
            print("### ",end="")
        elif num[i]=='7':
            print("### ",end="")
        elif num[i]=='8':
            print("### ",end="")
        elif num[i]=='9':
            print("### ",end="")
    print("")
    for i in range(len(num)):
        if num[i]=='0':
            print("# # ",end="")
        elif num[i]=='1':
            print("  # ",end="")
        elif num[i]=='2':
            print("  # ",end="")
        elif num[i]=='3':
            print("  # ",end="")
        elif num[i]=='4':
            print("# # ",end="")
        elif num[i]=='5':
            print("#   ",end="")
        elif num[i]=='6':
            print("#   ",end="")
        elif num[i]=='7':
            print("  # ",end="")
        elif num[i]=='8':
            print("# # ",end="")
        elif num[i]=='9':
            print("# # ",end="")
    print("")
    for i in range(len(num)):
        if num[i]=='0':
            print("# # ",end="")
        elif num[i]=='1':
            print("  # ",end="")
        elif num[i]=='2':
            print("### ",end="")
        elif num[i]=='3':
            print("### ",end="")
        elif num[i]=='4':
            print("### ",end="")
        elif num[i]=='5':
            print("### ",end="")
        elif num[i]=='6':
            print("### ",end="")
        elif num[i]=='7':
            print("  # ",end="")
        elif num[i]=='8':
            print("### ",end="")
        elif num[i]=='9':
            print("### ",end="")
    print("")
    for i in range(len(num)):
        if num[i]=='0':
            print("# # ",end="")
        elif num[i]=='1':
            print("  # ",end="")
        elif num[i]=='2':
            print("#   ",end="")
        elif num[i]=='3':
            print("  # ",end="")
        elif num[i]=='4':
            print("  # ",end="")
        elif num[i]=='5':
            print("  # ",end="")
        elif num[i]=='6':
            print("# # ",end="")
        elif num[i]=='7':
            print("  # ",end="")
        elif num[i]=='8':
            print("# # ",end="")
        elif num[i]=='9':
            print("  # ",end="")
    print("")
    for i in range(len(num)):
        if num[i]=='0':
            print("### ",end="")
        elif num[i]=='1':
            print("  # ",end="")
        elif num[i]=='2':
            print("### ",end="")
        elif num[i]=='3':
            print("### ",end="")
        elif num[i]=='4':
            print("  # ",end="")
        elif num[i]=='5':
            print("### ",end="")
        elif num[i]=='6':
            print("### ",end="")
        elif num[i]=='7':
            print("  # ",end="")
        elif num[i]=='8':
            print("### ",end="")
        elif num[i]=='9':
            print("### ",end="")
    print("")
print_number(int(input("Enter the number you wish to display: ")))

你们可以试一下output
Enter the number you wish to display: 9081726354
结果是运行正常的,格式也是对的,但怎样能把这段代码给简化

  • 写回答

3条回答 默认 最新

  • 关注

    改成这种的就可以,第一个循环的字典改好了,后面依次同理即可

    t1={'0':"### ",'1':"  # ",'2':"### ",'3':"  # ",'4':"# # ",'5':"### ",'6':"### ",'7':"### ",'8':"### ",'9':"### "}
    t2={'0':"### ",'1':"  # ",'2':"### ",'3':"  # ",'4':"# # ",'5':"### ",'6':"### ",'7':"### ",'8':"### ",'9':"### "}
    t3={'0':"### ",'1':"  # ",'2':"### ",'3':"  # ",'4':"# # ",'5':"### ",'6':"### ",'7':"### ",'8':"### ",'9':"### "}
    t4={'0':"### ",'1':"  # ",'2':"### ",'3':"  # ",'4':"# # ",'5':"### ",'6':"### ",'7':"### ",'8':"### ",'9':"### "}
    t5={'0':"### ",'1':"  # ",'2':"### ",'3':"  # ",'4':"# # ",'5':"### ",'6':"### ",'7':"### ",'8':"### ",'9':"### "}
    def print_number(num):
        num=str(num)
        for i in num :
            print(t1[i],end='')
        print("")
        for i in num:
            print(t2[i],end='')
        print("")
        for i in num:
            print(t3[i],end='')
        print("")
        for i in num:
            print(t4[i],end='')
        print("")
        for i in num:
            print(t5[i],end='')
        print("")
    print_number('01239')
    print_number(int(input("Enter the number you wish to display: ")))
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 6月21日
  • 已采纳回答 6月13日
  • 创建了问题 6月13日

悬赏问题

  • ¥15 onlyoffice编辑完后立即下载,下载的不是最新编辑的文档
  • ¥15 求caverdock使用教程
  • ¥15 Coze智能助手搭建过程中的问题请教
  • ¥15 12864只亮屏 不显示汉字
  • ¥20 三极管1000倍放大电路
  • ¥15 vscode报错如何解决
  • ¥15 前端vue CryptoJS Aes CBC加密后端java解密
  • ¥15 python随机森林对两个excel表格读取,shap报错
  • ¥15 基于STM32心率血氧监测(OLED显示)相关代码运行成功后烧录成功OLED显示屏不显示的原因是什么
  • ¥100 X轴为分离变量(因子变量),如何控制X轴每个分类变量的长度。