在python中列出二进制转换器

时间:2015-02-26 20:57:58

标签: python list binary

我在python中完成了一个在线初学者课程我的任务之一是设计一个简单的十进制到二进制到十六进制转换器。我写了初始代码,但我有点难以并排显示列表。这就是课程所说的:

1打印值 (对于此选项显示“denary binary hex”的标题,并使用您的列表打印一个表格,显示每个数字基数对应0到15的值,即

denary      binary      hexadecimal
0           0000            0
1           0001            1
etc

这是我写的代码。

binary_list=["0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"]
hex_list=[0,1,2,3,4,5,6,7,8,9,"A","B","C","D","E","F"]
decimal_list=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
print("What operation would you like to carry out today?")
option=input("Enter choice \n1. See a comparison of the first few decimal numbers converted to binary and hex values\n2. Convert denary number to binary or hex\n3. Convert a hex value between 0 and 15 to denary or binary value\n9. Quit")
print decimal_list
print hex_list 
print binary_list

把它们放到桌子里确实很有用,我真的不知道怎么做。我再次使用python作为第一个计时器,我只是想使用def语句等,所以如果可以,请使用基本编码。

谢谢大家

1 个答案:

答案 0 :(得分:0)

def toBinary(num):
    return "{0:b}".format(num)

def toHex(num):
    return '{:x}'.format(num)

我们无需存储binary_listhex_list,因为我们可以动态计算它们。