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?

value function does not exist, and it does not make sense to indent the 25-27 inside the definition of a class. You are obviusly not understanding what class is about please read up little more and read some code from code snippets archive like http://www.daniweb.com/software-development/python/code/431887/the-employee-class-record-revisited

Here something closer to your purpose, but using int, which you surely are not allowed to use (and no the Operand name for the class does not make any kind of sense)

class InvalidNumeralStringError(Exception):
    pass

class Number:
    def __init__(self, s):
        self = self.set(s)

    def set(self, s):
        self.convert, self.base = s.split('b')
        self.base = int(self.base)

    @property
    def value(self):
        return int(self.convert, self.base)

    def __int__(self):
        return self.value

    def __str__(self):
        return '%sb%i' % (self.convert, self.base)

    def __repr__(self):
        return "Number('%s')" % self

if __name__ == '__main__':
    s1 = '1101b3'
    b = Number(s1)
    print b.value
    b.set('123417b8')
    print repr(b),'=', int(b)

Edited 4 Years Ago by pyTony

It appears that the classes are part of the cause of your confusion, so since you do not use them, I have just deleted them and the code runs until it encounters the next error. Note that you will have a problem if you try to split anything after line #9 since "split" is now a variable, changed by the code to no longer be a fuction that splits into parts.

def value(s):
    split_s = s.split('b')
    convert = split_s[0]
    base = split_s[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(range(len(s))):  ## range is already a list
        total += v[s[i]] * (base**exp)
        exp += 1
    return total

if __name__ == '__main__':
    s1 = '1101b2'
    value(s1)

Edited 4 Years Ago by woooee

This article has been dead for over six months. Start a new discussion instead.