Before I start anything I just want to say that I know about the
hex()
function and for this project I can't use it. Also with regards to the question asked here, I'm aware of it and I've tried that solution but could not get my code to work. It is a different situation as I am using multiple functions. That thread also does not discuss my second question.
I've got two problems:
- The code I have so far can turn a decimal into a hexadecimal. Where I get stuck is that when I print the hexadecimal it prints backwards.
- I want the output to read:
Enter decimal value: 589
589 is equal to 24D in hexadecimal
But when I have the line:
print(og_dec_value,"is equal to",getHexChar(hex_value),end="","in hexadecimal")
I get an error about end=""
not being at the end. But if I remove end=""
then it only prints out the first number of the hexadecimal and leaves out the rest.
Here's my code as it stands right now:
def main():
decToHex(int(input("Enter decimal value: ")))
def decToHex(dec_value):
while dec_value > 0:
hex_value=dec_value%16
dec_value=dec_value//16
print(getHexChar(hex_value),end="")
def getHexChar(dec_digit):
if dec_digit < 10:
return dec_digit
if dec_digit == 10:
return "A"
if dec_digit == 11:
return "B"
if dec_digit == 12:
return "C"
if dec_digit == 13:
return "D"
if dec_digit == 14:
return "E"
if dec_digit == 15:
return "F"
main()