Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m trying to write a function that converts any decimal number you input into

ID: 3620387 • Letter: I

Question

I'm trying to write a function that converts any decimal number you input into hexadecimal. Here's what I got so far, and I'm trying to get "nextDigit" to make it so that any number 10-15 shows up as letter A-F. What do I need to write in order to do that?

def Dec2Hex(n):
Hex = ' '
if n < 0: return 'error'
if n == 0: return '0'
while n > 0:
nextDigit = n % 16
if nextDigit == 10:
nextDigit = 'A'
Hex = str(n % 16) + Hex
n = n // 16
return Hex

Explanation / Answer

Dear... def dec2hex(dec): hexconv=[ ] hexadec=[ ] n=0 while float(dec/(n+1))>=1: hexconv[n]=16^n n+=1 remainder=float(dec) while n!=0: hexadec[n]=int(remainder/hexconv[n]) remainder=(remainder%hexconv[n]) n-=0 final ="" for n in hexadec: if hexadec[n]==10: hexadec[n]=A elif hexadec[n]==11: hexadec[n]=B elif hexadec[n]==12: hexadec[n]=C elif hexadec[n]==13: hexadec[n]=D elif hexadec[n]==14: hexadec[n]=E elif hexadec[n]==15: hexadec[n]=F final = final + str(hexadec[n]) return final