鱼C论坛

 找回密码
 立即注册
查看: 1293|回复: 0

[技术交流] 【课后作业】10-16-8-2进制转换

[复制链接]
发表于 2018-4-26 20:33:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
动动手:
请输入一个整数(输入Q结束程序): 108
十进制->十六进制:108 ——> 0x6c
十进制->八进制: 108 -> 0o154
十进制->二进制: 108 ->0b110100
请输入一个整数(输入Q结束程序): 108

思路:
1. 16进制转换函数 hex(), 2进制转换函数 bin(), 八进制转换函数;
2. 输入中进行一个判断,是否Q,如果是跳出循环并结束程序;
3. 调用3个进制转换函数依次把转换后的值打印出来;

#Homework for the 15th course#
#Exchange a int number to other different type#

temp = input("please input a number(Q quit the program):")
if temp == "Q":
    print('Exit the program')
    exit()
else:
    number = int(temp)
    print (number)
    num_hex = hex(number);print(num_hex)
    num_oct = oct(number);print(num_oct)
    num_bin = bin(number);print(num_bin)
    print("十进制->十六进制:%d -> %s" % (number, num_hex))
    print("十进制->八进制:%d -> %s" % (number, num_oct))
    print("十进制->二进制:%d -> %s" % (number, num_bin))

===Split Line===
Connected to pydev debugger (build 162.1967.10)
please input a number(Q quit the program):Q
Exit the program

Process finished with exit code 0

===Split Line===
Connected to pydev debugger (build 162.1967.10)
please input a number(Q quit the program):120
120
0x78
0o170
0b1111000
十进制->十六进制:120 -> 0x78
十进制->八进制:120 -> 0o170
十进制->二进制:120 -> 0b1111000

Process finished with exit code 0
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 22:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表