索引超出范围误差函数roman到数字。我知道它为什么会发生,但不知道如何解决它

时间:2013-02-25 00:22:11

标签: python-2.7

''' Convert Arabic to Roman numeral and vice versa. '''

def arabic_to_roman(number):
    ''' Converts an integer to a Roman numeral.  Notice
        the patterns for computing M, C, and X are the
        same.  Computing D, L, and V follow the same pattern.
    '''
    output_string = ''

    # compute M
    roman = number / 1000
    if number >= 900 and number < 1000:
        output_string += 'CM'
        number -= 900
    else:
        output_string += roman * 'M' 
        number -= roman * 1000
        if number >= 900 and number < 1000:
            output_string += 'CM'
            number -= 900

    # compute D
    if number >= 400 and number < 900:
        if number < 500: 
            output_string += 'CD'
            number -= 400
        else:
            output_string += 'D'
            number -= 500


    # compute C
    roman = number / 100
    if number >= 90 and number < 100:
        output_string += 'XC'
        number -= 90
    else:
        output_string += roman * 'C'
        number -= roman * 100
        if number >= 90 and number < 100:
            output_string += 'XC'
            number -= 90

    # compute L
    if number >= 40 and number < 90:
        if number < 50:
            output_string += 'XL'
            number -= 40
        else:
            output_string += 'L'
            number -= 50

    # compute X
    roman = number / 10
    if number == 9:
        output_string += 'IX'
        number -= 9
    else:
        output_string += roman * 'X'
        number -= roman * 10
    if number == 9:
        output_string += 'IX'
        number -= 9

    # compute V
    if number >= 4 and number < 9:
        if number == 4:
            output_string += 'IV'
            number -= 4 
        else:
            output_string += 'V'
            number -= 5

    # compute I
    output_string += number * 'I'
    number -= number

    # output the result of the conversion 
    print '\nRoman numeral:', output_string 


def roman_to_arabic(num_string):
    ''' Convert a Roman numeral to an Arabic numeral. '''

    result = 0
    result1 = 0
    result2 = 0
    result3 = 0
    now = 0
    previous = 0
    index = -1

    ''' <Enter your code here ...> '''

    for i in (num_string):
        x = num_string[index]
        y = num_string[index-1]
        if x == 'I':
            now = 1
        if x == 'V':
            now = 5
        if x == 'X':
            now = 10
        if x == 'L':
            now = 50
        if x == 'C':
            now = 100
        if x == 'D':
            now = 500
        if x == 'M':
            now = 1000
        if y == 'I':
            previous = 1
        if y == 'V':
            previous = 5
        if y == 'X':
            previous = 10
        if y == 'L':
            previous = 50
        if y == 'C':
            previous = 100
        if y == 'D':
            previous = 500
        if y == 'M':
            previous = 1000
        if y == ' ':
            previous = 0
        if previous < now:
            result1 = now - previous
            print result1
            result += result1
            index += -1
        elif previous == now:
            result2 = previous + now
            print result2
            result += result2
            index += -1
        else:
            result3 = now
            print result3
            result += result3
        index += -1
        print '\nArabic numeral:', result
    print '\nArabic numeral:', result


def main():
    ''' The program driver. '''

    conversion_option = raw_input('Conversion Options: \n    (1) Arabic to Roman \n    (2) Roman to Arabic \nYour choice? ')

    print # prints an empty line 
    if conversion_option == '1':
        arabic_num = int(raw_input('Enter an Arabic numeral between 1 and 3999: '))
        arabic_to_roman(arabic_num)
    else:
        string_num = raw_input('Enter a Roman numeral between I and MMMCMXCIX: ')
        string_num = " " + string_num
        roman_to_arabic(string_num)

main()

0 个答案:

没有答案