我有一段试卷中的代码,我正在做一些额外的帮助。
我需要帮助的代码:
def Denary(Hex):
Result = ''
ErrorFound = False
DenaryEquivalent = ''
EmptyInput=""
for ThisHexDigit in Hex:
if ThisHexDigit in ['1','2','3','4','5','6','7','8','9','0','A','B','C','D','E','F']:
if ThisHexDigit == '0': DenaryEquivalent = '0'
elif ThisHexDigit == '1': DenaryEquivalent = '1'
elif ThisHexDigit == '2': DenaryEquivalent = '2'
elif ThisHexDigit == '3': DenaryEquivalent = '3'
elif ThisHexDigit == '4': DenaryEquivalent = '4'
elif ThisHexDigit == '5': DenaryEquivalent = '5'
elif ThisHexDigit == '6': DenaryEquivalent = '6'
elif ThisHexDigit == '7': DenaryEquivalent = '7'
elif ThisHexDigit == '8': DenaryEquivalent = '8'
elif ThisHexDigit == '9': DenaryEquivalent = '9'
elif ThisHexDigit == 'A': DenaryEquivalent = '10'
elif ThisHexDigit == 'B': DenaryEquivalent = '11'
elif ThisHexDigit == 'C': DenaryEquivalent = '12'
elif ThisHexDigit == 'D': DenaryEquivalent = '13'
elif ThisHexDigit == 'E': DenaryEquivalent = '14'
elif ThisHexDigit == 'F': DenaryEquivalent = '15'
elif ThisHexDigit == '10': DenaryEquivalent = '16'
elif Hex==EmptyInput:
print('Empty input, try again.')
Result = Result + DenaryEquivalent
else:
ErrorFound == True
print('You have made a mistake')
def HexToDenary():
Hexadecimal = input('Enter a hexadecimal number: ')
Converted = Denary(Hexadecimal)
print (Converted)
运行时,没有错误并且程序可以运行(这只是其中的一部分)。我想知道的是如何添加结果而不是显示为二进制答案。例如,如果我输入“BB”,我会得到“1111”而不是 187。这应该很容易,但我想不通。
我知道这是一段过于复杂的代码,但它就是给出的。