Skip to content

Instantly share code, notes, and snippets.

@TheRolfFR
Created February 14, 2022 17:01
Show Gist options
  • Save TheRolfFR/3b68a6034c7381e040c16fcda86d0f56 to your computer and use it in GitHub Desktop.
Save TheRolfFR/3b68a6034c7381e040c16fcda86d0f56 to your computer and use it in GitHub Desktop.
Number to roman numeral converter
romlist = ["M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"]
numlist = [1000,900,500,400,100,90,50,40,10,9,5,4,1]
number = int(input("Enter integer > 0: "))
rn = []
numlist_index = 0
# a number should
while number > 0:
# add the roman numeral while it "fits"
if numlist[numlist_index] <= number:
rn.append(romlist[numlist_index]) # add numeral to list
number -= numlist[numlist_index] # remove amount from number
else:
numlist_index += 1 # else move to smaller one
print(rn)
print(''.join(rn))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment