I'm fairly new in python. This code is supposed to create an Operand class where the function is take a string like 563b9, split it so that the integer is 563 and the base is 9 and then convert it to decimal. All I have done is split the string and assign them proper names and then attempt to run another function within the function.
class InvalidNumeralStringError(Exception):
pass
class Operand:
def __init__(self):
self._data = []
def value(s):
split = s.split('b')
convert = split[0]
base = split[1]
convert_to_decimal(convert, base)
def convert_to_decimal(s, base):
v = {'0':0, '1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9,
'A':10, 'B':11, 'C':12, 'D':13, 'E':14, 'F':15}
total = 0
exp = 0
for i in reversed(list(range(len(s)))):
total += v[s[i]] * (base**exp)
exp += 1
return total
if __name__ == '__main__':
s1 = '1101b2'
value(s1)
Traceback (most recent call last):
File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 4, in <module>
#print 'Exiting sandbox process'
File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 27, in Operand
File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 12, in value
builtins.NameError: global name 'convert_to_decimal' is not defined
I'm not sure what the error for global name is not defined means. The function is there with the class? Also, is this supposed to be how I create a testing block for a Class? I hadn't indented it before and it wasn't working, it has to be inside the Class itself?