如何将遵循罗马数字规则的罗马数字从I转换为M?

时间:2019-05-18 01:34:35

标签: python

我需要按照罗马数字规则将罗马数字从I转换为M:最多重复3次该数字,仅I,X,C和M可以重复; V,L和D不能,也不需要这样做。等等...

val = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}

def romantoint():
    helloinput = str(input('Enter a roman numeral: '))
    helloinput = helloinput.upper()
    total = 0
    while helloinput:
        if len(helloinput) == 1 or val[helloinput[0]] >= val[helloinput[1]]:
            total += val[helloinput[0]]
            helloinput = helloinput[1:]
        else:
            total += val[helloinput[1]] - val[helloinput[0]]
            helloinput = helloinput[2:]
    print(total)
romantoint()

预期结果是在输入罗马数字时以正确的罗马数字规则打印整数。我尝试了许多应该被认为是错误的数字,但是它在我的机器上仍然有效。

0 个答案:

没有答案