现要求使用Python语言编写函数实现十进制至十六进制的转换,并绘制数码管将十进制及其对应的十六进制显示出来。
输入:
9999
输出:
![](https://profile-avatar.csdnimg.cn/default.jpg!4)
用Python实现十至十六进制的转换,并绘制七段数码管显示出来。
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答 默认 最新
关注
我帮你写了一版,供你参考。
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()
运行效果图:
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 3无用
悬赏问题
- ¥15 metadata提取的PDF元数据,如何转换为一个Excel
- ¥15 关于arduino编程toCharArray()函数的使用
- ¥100 vc++混合CEF采用CLR方式编译报错
- ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
- ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
- ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
- ¥50 如何openEuler 22.03上安装配置drbd
- ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
- ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
- ¥15 Windows11, backspace, enter, space键失灵