-2

Im completing a online beginner course in python one of my tasks is to design a simple decimal to binary to hex converter. Ive written the initial code but im a bit stumped on displaying the lists side by side. This is what the course says:

1 Print values (for this option display a heading for “denary binary hex” and use your lists to print a table showing the values corresponding to 0 to 15 for each number base i.e.

denary      binary      hexadecimal
0           0000            0
1           0001            1
etc

This is the code Ive written.

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

It really dosent work to put them into a table, and im not really sure how to do it. Again I am a first timer with python, and im only meant to be using def statements etc so if you could, please use basic coding.

Thank you all

Mazdak
  • 105,000
  • 18
  • 159
  • 188
Andy Orchard
  • 15
  • 1
  • 7
  • 2
    This assignment looks [awfully familiar](http://stackoverflow.com/questions/28752599/list-in-python-for-binary-converter). But yesterday you were a 'tutor'. – Martijn Pieters Feb 26 '15 at 21:08
  • @Martijn: The "awfully familiar" link in your comment is to _this_ question. – martineau Feb 26 '15 at 21:16
  • 1
    @martineau: I was just about to correct that: [Python - Binary to Hex to Denary converter](http://stackoverflow.com/q/28727916). The similarities go beyond the assignment contents. – Martijn Pieters Feb 26 '15 at 21:16

1 Answers1

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

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

There is no need to store binary_list and hex_list as we can dynamically compute them.

taesu
  • 4,482
  • 4
  • 23
  • 41