Answer the question
In order to leave comments, you need to log in
Converting roman numbers to arabic (error in loop)?
Task: Write a small program that converts Roman numbers containing I, V, X to Arabic numbers
I am currently learning python but have been playing around with java for a while and if I understand some things, there is still a lot to learn. In particular, I do not really understand how to work with cycles. How in this example to implement what I want.
The program throws an error because of (a[i+1]) - I want to get the next character in variable a. I understand why such an error appears, but I do not understand how to get around it. Help me please.
The code:
a = input()
number = 0
for i in a:
if i=='I':
if a[i+1]=='V':
number+=4
elif a[i+1]=='X':
number+=9
else:
number+=1
elif i=='V':
number+=5
elif i=='X':
number+=10
else:
print("Something is broken")
print(number)
waiter=input()
Answer the question
In order to leave comments, you need to log in
Use enumerate , which will allow you to have both the index and the value of an element. Then the cycle can be written like this:
for index, literal in enumerate(a):
pass
#!/usr/bin/env python
rule_add = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000,
}
rule_div = {
('I', 'V'): 3,
('I', 'X'): 8,
('X', 'L'): 30,
('X', 'C'): 80,
('C', 'D'): 300,
('C', 'M'): 800,
}
def roman_to_arabic(roman_number):
number = 0
prev_literal = None
for literal in roman_number:
if prev_literal and rule_add[prev_literal] < rule_add[literal]:
number += rule_div[(prev_literal, literal)]
else:
number += rule_add[literal]
prev_literal = literal
return number
import unittest
class RomanNumTest(unittest.TestCase):
def test_roman_num(self):
self.assertEquals(roman_to_arabic('I'), 1)
self.assertEquals(roman_to_arabic('II'), 2)
self.assertEquals(roman_to_arabic('III'), 3)
self.assertEquals(roman_to_arabic('IV'), 4)
self.assertEquals(roman_to_arabic('V'), 5)
self.assertEquals(roman_to_arabic('VI'), 6)
self.assertEquals(roman_to_arabic('VII'), 7)
self.assertEquals(roman_to_arabic('VIII'), 8)
self.assertEquals(roman_to_arabic('IX'), 9)
self.assertEquals(roman_to_arabic('X'), 10)
self.assertEquals(roman_to_arabic('XXXI'), 31)
self.assertEquals(roman_to_arabic('XLVI'), 46)
self.assertEquals(roman_to_arabic('XCIX'), 99)
self.assertEquals(roman_to_arabic('DLXXXIII'), 583)
self.assertEquals(roman_to_arabic('DCCCLXXXVIII'), 888)
self.assertEquals(roman_to_arabic('MDCLXVIII'), 1668)
self.assertEquals(roman_to_arabic('MCMLXXXIX'), 1989)
self.assertEquals(roman_to_arabic('MMX'), 2010)
self.assertEquals(roman_to_arabic('MMXI'), 2011)
self.assertEquals(roman_to_arabic('MMXII'), 2012)
self.assertEquals(roman_to_arabic('MMMCMXCIX'), 3999)
Organize the loop in a different way, so that it is clear which character of the string is being analyzed. Add a condition that compares the length of the string and the index of this character - if the character is the last in the string - all conditions with a[i+1] should not be checked.
The i in the loop is not the number of the current character, but the character itself.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question