Search code examples
pythonroman-numerals

Python 3.5.2 Roman Numeral Quiz


So I am doing this assignment for school. I have to create a quiz like game that will prompt a user to add roman numerals together and input their answer. It will then check the user's answer with the correct answer and tell the user if they got it right or wrong.

So far I have this:

class RomanNumeral:
    index = 0
    while index < len(integer_list) - 1:
        #if a lower number is first, it should be subtracted, like IX, 
        #the one should be subtracted, but the 10 added
        if integer_list[index] < integer_list[index + 1]:
            r_integer -= integer_list[index]
        else:
            r_integer += integer_list[index]
        index += 1
    #Always add the last number
    r_integer += integer_list[index]
    #Store r_integer as an instance data item
    self.__integer = r_integer
    return

def main():
        roman1 = RomanNumeral('random')
        roman2 = RomanNumeral('random')
        correct_answer = roman1 + roman2
main()

But when I run it, i get this error:

r_integer += integer_list[index]
UnboundLocalError: local variable 'r_integer' referenced before assignment

Any suggestions on how to fix this issue?

As well, I need help overloading the int method to change the roman numerals to integers so that they can be added together.


Solution

  • You aren't using the integer defined for self. Try adding a declaration after

     r_string = r_string.upper()
    

    Add

    r_integer = self.__integer
    

    That way you have a local copy to work with.

    However you do need to overload the integer method which is answered in this post