Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Language: Python 3 Roman numerals use symbols M, D, C, X, V, and I whose decimal

ID: 673225 • Letter: L

Question

Language: Python 3

Roman numerals use symbols M, D, C, X, V, and I whose decimal values
are M = 1000, D = 500, C = 100, X = 10, V = 5, I = 1. For example, the
Roman numeral MDCXVII corresponds to 1000+500+100+10+5+1+1=1617. There
are more complicated rules, e.g. IV usually is 4, but we'll use a
simple version of Roman numerals where we just accumulate the values
of all symbols. E.g. MIIIMMDCM we'll evaluate as 4*1000 + 3*1 + 1*500
+ 1*100 = 4603. Write a function called roman_v2 that asks a user to
enter a roman number using capital letters M, D, C, X and I and
returns a decimal numeral computed according to the above simplified rules.

You cannot use python methods (those called with dot operator) but rather
once you get input from the user,find a solution using only loops,
variables and if statements.

Testing function examples:

>>> roman_v2()
Enter a roman number using capital letters M, D, C, X and I: MIIIMMDCM
4603
>>> roman_v2()
Enter a roman number using capital letters M, D, C, X and I: IV
6
>>> roman_v2()
Enter a roman number using capital letters M, D, C, X and I: MDCXVII
1617
>>>
>>> roman_v2()
Enter a roman number using capital letters M, D, C, X and I: IIIVIII
11

Explanation / Answer

def roman(string):

table=[['M',1000],['CM',900],['D',500],['CD',400],['C',100],['XC',90],['L',50],['XL',40],['X',10],['IX',9],['V',5],['IV',4],['I',1]]
returnint=0
for pair in table:


continueyes=True

while continueyes:
if len(string)>=len(pair[0]):

if string[0:len(pair[0])]==pair[0]:
returnint+=pair[1]
string=string[len(pair[0]):]

else: continueyes=False
else: continueyes=False

return returnint