输入分配不正确

时间:2018-04-22 01:05:35

标签: python

def roman_value(roman):

    roman_values = (('I',1), ('IV',4), ('V',5), ('IX',9),('X',10),('XL',40),('L',50),('XC',90),('C',100),
                    ('CD', 400), ('D', 500), ('CM', 900), ('M',1000))

    romanSymbol = input("enter roman symbols < 3999: ")

    total=0

    for symbol,value in reversed(roman_values):
        while roman.startswith(symbol):
            total += value
            roman = roman[len(symbol):]
    return total

if __name__=='__main__':
    for value in "MCMXC", "MMVIII", "MDCLXVI", "":
        print('%s = %i' % (value, roman_value(value)))

roman_value()

1 个答案:

答案 0 :(得分:0)

我认为你真的想要这样的东西:

def roman_value():

    roman_values = (('I',1), ('IV',4), ('V',5), ('IX',9),('X',10),('XL',40),('L',50),('XC',90),('C',100),
                    ('CD', 400), ('D', 500), ('CM', 900), ('M',1000))

    romanSymbol = input("enter roman symbols < 3999: ")
    roman = romanSymbol

    total=0

    for symbol,value in reversed(roman_values):
        while roman.startswith(symbol):
            total += value
            roman = roman[len(symbol):]

    print('%s = %i' % (romanSymbol, total))

roman_value()