I am writing a function intToRoman to convert numbers to roman numerals:
class Solution:
def intToRoman(self, num: int) -> str:
#Dictionary with keys for symbol and value for value
symbols = {
'I' : 1,
'V' : 5,
'X' : 10,
'L' : 50,
'C' : 100,
'D' : 500,
'M' : 1000
}
str_form = str(num) #Get string form for extracting starting digit
first_digit = str_form[0] # Get the first digit in string form for comparisons
if first_digit != '4' and first_digit != '9': #If the value does not start with 4 or 9
symbol_list = []
for key,value in symbols.items(): # loop through to find the greatest key that does not return negative difference
if (num >= value ):
best_key = key
else:
break;
symbol_list += best_key # append this best key to the list
number_to_subtract = symbols[best_key]
num = num - number_to_subtract # subtract the value from the number
symbol_list.append(self.intToRoman(num)) #recursion
return ''.join(symbol_list)
if (first_digit == '4' or first_digit == '9'): #If the value starts with 4 or 9
symbol_list = []
if (len(str_form) == 1 and first_digit == '4'):
key_to_add = 'IV'
elif ((len(str_form) == 1) and first_digit == '9'):
key_to_add = 'IX'
elif ((len(str_form) == 2) and first_digit == '4'):
key_to_add = 'XL'
elif ((len(str_form) == 2) and first_digit == '9'):
key_to_add = 'XC'
elif ((len(str_form) == 3) and first_digit == '4'):
key_to_add = 'CD'
elif ((len(str_form) == 3) and first_digit == '9'):
key_to_add = 'CM'
symbol_list += key_to_add #append to symbol_list
if (len(str_form) > 1):
truncated_str_num = str_form[1:] #get the truncated str num ; everything except first digit
truncated_num = int(truncated_str_num)
symbol_list.append(self.intToRoman(truncated_num)) #recursion
return ''.join(symbol_list)
else:
return ''.join(symbol_list)
def main():
solution = Solution()
num = 3749
expected = "MMMDCCXLIX"
result = solution.intToRoman(num)
print(f"Test case 3: num = {num}, expected = {expected}, result = {result}")
num = 1994
expected = "MCMXCIV"
result = solution.intToRoman(num)
print(f"Test case 3: num = {num}, expected = {expected}, result = {result}")
num = 58
expected = "LVIII"
result = solution.intToRoman(num)
print(f"Test case 3: num = {num}, expected = {expected}, result = {result}")
if __name__ == "__main__":
main()'
For inputs that end with digits concerning the second if statement (starting with 4 or 9) it seems to work fine; see 3749 and 1994 above. But for inputs like 58, or even simpler just inputs like 8, or 7, I get the error:
File “intToRoman.py”, line 27, in intToRoman
symbol_list += best_key # append this best key to the list
^^^^^^^^^^^
UnboundLocalError: cannot access local variable ‘best_key’ where it is not associated with a value
I don’t understand how this is happening since best_key should be initialized no matter what in this clause:
if (num >= value ):
best_key = key
Since there is no number that doesn’t satisfy this condition. Is this my fault for not initializing best_key before entering the loop?
any help would be appreciated
Z10 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.