1

So, I've attempted to make a simple Base 10 to Base 16 converter (I realize this is Base 10 integer to a mock-Base 16 string). I'm fairly new to Python; I just started learning it this week (I have prior programming experience with C#, so I'm not lost on this).

R1 = 0
R2 = 0
R3 = 0
R4 = 0
R5 = 0

number = int(raw_input("Enter a base 10 number (Smaller than 1048576): "))

def numberConvert(a=0):
  a = str(a)

  if (a == "10"):
    a = "a"
  elif (a == "11"):
    a = "b"
  elif (a == "12"):
    a = "c"
  elif (a == "13"):
    a = "d"
  elif (a == "14"):
    a = "e"
  elif (a == "15"):
    a = "f"
  else:
    a = a

#Dividing

Q1 = (number // 16)
R1 = (number % 16)

Q2 = (Q1 // 16)
R2 = (Q1 % 16)

Q3 = (Q2 // 16)
R3 = (Q2 % 16)

Q4 = (Q3 // 16)
R4 = (Q3 % 16)

Q5 = (Q4 // 16)
R5 = (Q4 % 16)

R1 = numberConvert(R1)
R2 = numberConvert(R2)
R3 = numberConvert(R3)
R4 = numberConvert(R4)
R5 = numberConvert(R5)

print R1 + R2 + R3 + R4 + R5

After running and inputting 1048575 (that's the highest it can go, so I'm expecting "fffff" as an output), it gives me:

C:\PythonPrograms\Modules>Decimal_to_Hex.py
Enter a base 10 number (Smaller than 1048576): 1048575
Traceback (most recent call last):
  File "C:\PythonPrograms\Modules\Decimal_to_Hex.py", line 50, in <module>
    print R1 + R2 + R3 + R4 + R5
TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType'

After I ran some tests which mainly involved printing the R1 to R5 values at the beginning and the end of the function, I've come to the conclusion that the issue lies somewhere in the R1 = numberConvert(R1) section. At any time before that, the variables behave normally. Right at the start of the function, after being inputted, theR variables are numbers 0 through 15.

Right before function numberConvert finishes up, they are 0 through 9, or "a" through "f". However, if I were to print value R1 immediately after R1 = numberConvert(R1), it gives me None as an output.

From what I've read, this is essentially Python's null value. So, I've narrowed down the problem to the section where I'm running the function numberConvert.

1 Answers1

6

All functions in Python return None by default unless you have a return-statement. So, if you want your numberConvert function to return a value other than None, you need to put one:

def numberConvert(a=0):
  a = str(a)

  if (a == "10"):
    a = "a"
  elif (a == "11"):
    a = "b"
  elif (a == "12"):
    a = "c"
  elif (a == "13"):
    a = "d"
  elif (a == "14"):
    a = "e"
  elif (a == "15"):
    a = "f"
  else:
    a = a

  ########
  return a
  ########

Since you say you have C# experience, perhaps you thought that doing:

R1 = numberConvert(R1)

would pass R1 as a reference which would be modified inside numberConvert whenever you did:

a = ...

This is incorrect though because a is treated as a local name that gets assigned the value of R1. For more information on this, see How do I pass a variable by reference?


Also, there are a few things I'd like to mention about your numberConvert function:

  1. You do not need parenthesis with if-statements in Python. In fact, most Python programmers consider them to be bad style. Instead, you should just do:

    if a == "10":
      a = "a"
    elif a == "11":
      a = "b"
    ...
    
  2. There is not really a reason to do:

    a = str(a)
    

    since you can compare with integers directly:

    if a == 10:
      a = "a"
    elif a == 11:
      a = "b"
    
  3. If you want, you could use a dictionary to avoid all those if-statements:

    def numberConvert(a=0):
        return {"10":"a","11":"b","12":"c","13":"d","14":"e","15":"f"}.get(a, a)
    
Community
  • 1
  • 1