用Python实现十至十六进制的转换,并绘制七段数码管显示出来。

现要求使用Python语言编写函数实现十进制至十六进制的转换,并绘制数码管将十进制及其对应的十六进制显示出来。
输入:
9999
输出:

img

我帮你写了一版,供你参考。

import turtle

t = turtle.Turtle()

CHAR_MAP = {
    '0': 0b0111111,
    '1': 0b0000110,
    '2': 0b1011011,
    '3': 0b1001111,
    '4': 0b1100110,
    '5': 0b1101101,
    '6': 0b1111101,
    '7': 0b0000111,
    '8': 0b1111111,
    '9': 0b1101111,
    'a': 0b1110111,
    'b': 0b1111100,
    'c': 0b0111001,
    'd': 0b1011110,
    'e': 0b1111001,
    'f': 0b1110001,
    'h': 0b1110110,
}


def draw_digit(c):
    char = CHAR_MAP[c]
    for i in range(7):
        b = char & 0b0000001
        t.penup()
        t.forward(5)
        if b:
            t.pendown()
        else:
            t.penup()
        t.forward(25)
        t.penup()
        t.forward(5)
        if i == 5:
            t.penup()
            t.backward(35)
        if i in [0, 2, 3, 5]:
            t.right(90)
        char = char >> 1


def draw_number(s):
    for c in s:
        draw_digit(c)
        t.penup()
        t.left(90)
        t.forward(35)
        t.right(90)
        t.forward(10)


def draw(x, y, label, num_str, postfix, postfix_color):
    t.penup()
    t.goto(x, y)
    t.color("black")
    t.pendown()
    t.write(label, move=False, align='right', font=('宋体', 12, 'normal'))
    t.penup()
    t.goto(x, y + 40)
    draw_number(num_str)
    t.color(postfix_color)
    draw_digit(postfix)


def main(dec_str):
    hex_str = str(hex(eval(dec_str)))
    draw(-250, 260, "十进制:", dec_str, 'b', 'red')
    draw(-250, 160, "十六进制:", hex_str[2:], 'h', 'green')


if __name__ == "__main__":
    t.speed(0)
    t.pensize(5)
    t.hideturtle()
    n = input()
    main(n)
    turtle.done()

运行效果图:

img

是用函数实现,还是写一个GUI界面啊

你的数码管是共阴还是共阳啊?实现上译码是不一样的啊