I'm practicing recursive functions and I would be glad to receive some feedback for the following Python code I wrote. I did several tests and the function yielded correct results but I wonder if it can be written more concise or if it can be criticised in some way. Your feedback would help me become better. The functionality of the function is described in the docstring.
def roman(n):
"""Takes a roman number n as an argument
(roman(n) assumes that the string n is
correctly written, i.e. it provides no
error-checking) and returns an integer."""
#The base-cases:
if n == "":
return 0
elif n == "M":
return 1000
elif n == "D":
return 500
elif n == "C":
return 100
elif n == "L":
return 50
elif n == "X":
return 10
elif n == "V":
return 5
elif n == "I":
return 1
#If a smaller number precedes a bigger number,
#then the smaller number is to be subtracted from
#the bigger number. Else, it has to be added:
else:
if roman(n[0]) < roman(n[1]):
return (roman(n[1]) - roman(n[0])) + roman(n[2:])
else:
return roman(n[0]) + roman(n[1:])
def main(n):
print(roman(n))
main("MMMCMXCIX")