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
.