 @virnuls/

Roman Numerals Convert "normal" numbers into Roman numerals - also one of the OCR Coding Challenges

Files
• main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
digitval = (1000,500,100,50,10,5,1)
# NB. overlined digits can be used for larger
# values - see the following example:
# http://www.virnuls.co.uk/Romans/numerals.html
digit = ("M","D","C","L","X","V","I")
arabic = abs(int(input("Give me a whole number: ")))
roman = ""
print("In Roman numerals,",arabic,"is represented by:")
# Starting with the largest values, see how many times each one will fit into the number
for n in range(7):
# try each Roman number, starting with the largest
# e.g. if it's bigger than 1000, add an M
while arabic >= digitval[n]:
roman = roman + digit[n]
arabic = arabic - digitval[n]
# Unless we're down to 1, try the current value minus the next one - e.g. 4 if IV
if n < 6:
# For 1000, 100 and 10 you skip 2 (which is what the % 2 does), e.g. 99 is XCIX rather than IC
if arabic >= digitval[n] - digitval[n+1+((n+1) % 2)]:
roman = roman + digit[n+1+((n+1) % 2)] + digit[n]
arabic = arabic - (digitval[n] - digitval[n+1+((n+1) % 2)])
print(roman)