0

I am doing an exam paper for revision. It's not a particular question I want help with, but I am unsure to why the program is outputting incorrectly when certain data is entered.

def Binary(Hex):
    Result = ''
    ErrorFound = False
    BinaryEquivalent = ''
    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': BinaryEquivalent = '0'
            elif ThisHexDigit == '1': BinaryEquivalent = '1'
            elif ThisHexDigit == '2': BinaryEquivalent = '2'
            elif ThisHexDigit == '3': BinaryEquivalent = '3'
            elif ThisHexDigit == '4': BinaryEquivalent = '4'
            elif ThisHexDigit == '5': BinaryEquivalent = '5'
            elif ThisHexDigit == '6': BinaryEquivalent = '6'
            elif ThisHexDigit == '7': BinaryEquivalent = '7'
            elif ThisHexDigit == '8': BinaryEquivalent = '8'
            elif ThisHexDigit == '9': BinaryEquivalent = '9'
            elif ThisHexDigit == 'A': BinaryEquivalent = '10'
            elif ThisHexDigit == 'B': BinaryEquivalent = '11'
            elif ThisHexDigit == 'C': BinaryEquivalent = '12'
            elif ThisHexDigit == 'D': BinaryEquivalent = '13'
            elif ThisHexDigit == 'E': BinaryEquivalent = '14'
            elif ThisHexDigit == 'F': BinaryEquivalent = '15'
            Result = Result + BinaryEquivalent
        elif ErrorFound == True:
            print('You have made a mistake')
        elif Hex==EmptyInput:
            print('Empty input, try again.')

    return Result

Yes, I know this is an over-complicated piece of code but it is in the exam paper so I have to use it. It came like that, except that all the BinaryEquivalent strings were BinaryEquivalent = '' instead of having numbers inside.

The problem is when I enter two characters when the program is displaying. For example, entering "BBB" will output 11, as will 'BBBBBB'.

4
  • 2
    "it is in the exam paper"? Everything about this code is wrongly conceived. Commented Apr 10, 2012 at 12:04
  • How about try: BinaryEquivalent=str(int(ThisHexDigit, 16)) catch ValueError: print "You have made a mistake." or something similar? (you have to add newlines but a comment cannot handle code formatting properly). Your code is ugly on so many frontiers that I suggest a rewrite. Commented Apr 10, 2012 at 12:05
  • Wow, that's some really ugly code! Not related to your question, but its not good to name variables starting with uppercase, e.g. Result would be better as result. See PEP8. Commented Apr 10, 2012 at 12:05
  • 1
    I "fixed" the indentation in the code, because I thought you just had problems posting it, but it seems likely that it was actually the problem you were having, based on the first couple of responses. Commented Apr 10, 2012 at 12:06

3 Answers 3

2

You should put return statement out of the for cycle.

Sign up to request clarification or add additional context in comments.

Comments

0

The return statement is inside the for loop and therefore only one iteration is done, it should be:

for ThisHexDigit in Hex:
    #code

return result

and not:

for ThisHexDigit in Hex:
    #code
    return result

Comments

0

Your solution is good only if your hex number is single hex digit. If you want to convert longer numbers you will have to do some corrections.

  1. Result can be simple int, start it with 0
  2. all BinaryEquivalent should be ints so use BinaryEquivalent = 0
  3. in for loop you must increase your result, use: Result = 16 * Result + BinaryEquivalent

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.