Roman numerals to Arabic numerals
The program will receive a number in Roman numerals, such as CLI, which is 151 in Arabic numerals.
Roman numerals use the above table to resemble to given numbers. If a smaller number is in front of a bigger number, it means to minus the small number from the bigger number. For example:
IV = 4, IX = 9So all I needed to do was separate each letter and assign them their values, then added it to a list. Then, I just need to check if the value in position i was less then the position in i+1, which if it was, I just add the value of i+1 minus the value of i to the overall counter, and if it wasn't, I just add the value i to the overall counter:
def roman_to_decimal():
roman = input("Enter your roman numeral: ")
roman_list = []
for x in range(0, len(roman)):
if roman[x] == "I":
roman_list.append(1)
elif roman[x] == "V":
roman_list.append(5)
elif roman[x] == "X":
roman_list.append(10)
elif roman[x] == "L":
roman_list.append(50)
elif roman[x] == "C":
roman_list.append(100)
elif roman[x] == "D":
roman_list.append(500)
elif roman[x] == "M":
roman_list.append(1000)
roman_list.append(0)
decimal = 0
i = 0
while i < len(roman_list)-1:
if roman_list[i] < roman_list[i+1]:
decimal += roman_list[i+1] - roman_list[i]
i += 2
else:
decimal += roman_list[i]
i += 1
print(decimal)
Comments
Post a Comment