2

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.

Brock
  • 125
  • 13

4 Answers4

0

Your error says

local variable 'r_integer' referenced before assignment

This means you tried to use a variable before you defined it. Define r_integer to 0 (or some other number) before the while loop and your problem should be fixed.

Neil A.
  • 841
  • 7
  • 23
0

You need to initialize r_integer before the while loop. Added to your code below the ##### comment

    #Add if the number is greater than the one that follows, otherwise
    #subtract r_integer = 0
    #Stands for roman integer or the integer equivalent of the roman string
    index = 0
    ##### Initialize r_integer before the while loop
    r_integer = 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]
ode2k
  • 2,653
  • 13
  • 20
  • I don't think reposting the entire code is necessary, just direct him to where to put the code. – Neil A. Sep 15 '16 at 00:30
  • When i do this, the output is 10 lines of what ever i initialize r_integer to + 1 (i.e. if i set r_integer = 0 then the output is then lines of I, if i set it to 2 then the output is ten lines of III) – Brock Sep 15 '16 at 00:34
0

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

Community
  • 1
  • 1
Derrick Cheek
  • 147
  • 1
  • 6
  • if i do that, i get the error 'RomanNumeral' object has no attribute '_RomanNumeral__integer' i think that if i can overload the __int__ method that it may fix these problems. But i dont know how to overload the __int_ method – Brock Sep 15 '16 at 01:15
  • My bad. I now see that you are conditionally applying the integer value of string only I'd it's random. – Derrick Cheek Sep 15 '16 at 01:27
  • Does this answer encompass your problem or are there still issues with it? – Derrick Cheek Sep 19 '16 at 17:43
  • No more issues, I had accidentally commented out the r_integer initialization and then overloaded the int method and it worked flawlessly. – Brock Sep 19 '16 at 20:13
  • Great! Glad it worked. Would you be able to accept my answer when you have a moment. Thanks. – Derrick Cheek Sep 19 '16 at 21:09
0

Here is my solution:

class Solution:
def romanToInt(self, s):
    """
    :type s: str
    :rtype: int
    """
    array_representation = []
    for i in range(len(s)):
        array_representation.append(s[i]);
    total = 0
    i = 0
    print(array_representation)
    while (i < len(array_representation)):

        if (i < len(array_representation) - 1 and val(array_representation[i + 1]) > val(array_representation[i])):
            total += (val(array_representation[i + 1]) - val(array_representation[i]))
            i = i + 2;
        else:
            total += val(array_representation[i]);
            i = i + 1;

    return total;

This is the helper function I used

def val(value):
if (value == "I"):
    return 1
if (value == "V"):
    return 5
if (value == "X"):
    return 10;
if (value == "L"):
    return 50;
if (value == "C"):
    return 100;
if (value == "D"):
    return 500;
if (value == "M"):
    return 1000;