input_num = input("What is the integer you'd like to convert to hexadecimal? ") def dec_hex_conv(input_num): while_num = input_num for_num = input_num hex_string = "" hex_digit_count = 1 while while_num / 16 > 0: while_num = while_num / 16 hex_digit_count = hex_digit_count + 1 for i in range(0, hex_digit_count): power = hex_digit_count - 1 divisor = 16 ** power power = power - 1 hex_num = for_num / divisor for_num = for_num % divisor if hex_num >= 0 and hex_num <= 9: hex_string = hex_string + str(hex_num) elif hex_num == 10: hex_string = hex_string + "A" elif hex_num == 11: hex_string = hex_string + "B" elif hex_num == 12: hex_string = hex_string + "C" elif hex_num == 13: hex_string = hex_string + "D" elif hex_num == 14: hex_string = hex_string + "E" else: hex_string = hex_string + "F" return hex_string print dec_hex_conv(input_num)
For values of 1 through 15, I get the correct answers
For 16, I correctly get 10
For 17, I get 10 (incorrect, should be 11)
When I input 255, I get F0 (incorrect again)
With 4095, I get F00 (which should be FFF) etc.
So somehow I've managed to screw up the code in a way that I only get zeros in the last digit(s) when I input a number larger than 16.
Trouble is, I'm very new to Python programming and can't quite trace where the program is going wrong.
Any advice, hints, or tiny code snippets to help me learn would be much appreciated!