解释罗马数字并返回数字值

时间:2018-09-16 02:02:41

标签: python python-3.x

print ('please enter a roman numeral')
romannum = input()
lenrm = len(romannum)
total = 0
for i in range (lenrm):
    numone = 0
    numtwo = 0

    if (romannum[i]).lower() == 'i':
        numone = numone + 1

    if (romannum[i]).lower() == 'v':
        numone = numone + 5

    if (romannum[i]).lower() == 'x':
        numone = numone + 10

    if (romannum[i + 1]).lower() == 'i':
        numtwo = numtwo + 1

    if (romannum[i + 1]).lower() == 'v':
        numtwo = numtwo + 5

    if (romannum[i + 1]).lower() == 'x':
        numtwo = numtwo + 10


    if numone < numtwo:
        total = total + (numtwo - numone)
    else:
        total = total + numone
        i += 1
print (total)

输入xiv,我收到一条错误消息

Traceback (most recent call last):
  File "C:\Users\poona\Python\roman_numeral_reader.py", line 18, in <module>
    if (romannum[i + 1]).lower() == 'i':
IndexError: string index out of range

4 个答案:

答案 0 :(得分:0)

因此您输入的长度为5,可以说IIIII,现在取range(5),这意味着它将迭代的最高值为4(从0到4,包括两者),现在索引4是您中的最后一个索引5索引长数字,因为我们从0开始对索引进行计数,因此当您将其加1时,将获得超出范围的索引5。

以您的情况

romannum = "xiv"
lenrm = 3
total = 0
for i in [0,1,2]: # for i in range(3):
    ...
    if (romannum[3]).lower() == 'i': # if (romannum[i + 1]).lower() == 'i':

答案 1 :(得分:0)

这看起来像是您正在尝试访问vxiv旁边的字符,当然这是超出范围的。

我建议在尝试确定下一个字符之前检查边界:

for i in range (lenrm):
    numone = 0
    numtwo = 0

    if (romannum[i]).lower() == 'i':
        numone = numone + 1

    if (romannum[i]).lower() == 'v':
        numone = numone + 5

    if (romannum[i]).lower() == 'x':
        numone = numone + 10

    if i + 1 < lenrm:  # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        if (romannum[i + 1]).lower() == 'i':
            numtwo = numtwo + 1

        if (romannum[i + 1]).lower() == 'v':
            numtwo = numtwo + 5

        if (romannum[i + 1]).lower() == 'x':
            numtwo = numtwo + 10

答案 2 :(得分:0)

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
    android:id="@+id/layout"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:orientation="horizontal"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <EditText
            android:id="@+id/txt"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:ems="10"
            android:inputType="number"
            android:maxLines="1"
            android:singleLine="true"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toStartOf="@id/txtDrawable"
            app:layout_constraintBottom_toBottomOf="@id/txtDrawable"/>

        <EditText
            android:id="@+id/txtDrawable"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/ic_android_black_24dp"
            android:drawablePadding="8dp"
            android:drawableStart="@drawable/ic_android_black_24dp"
            android:ems="10"
            android:inputType="number"
            android:maxLength="4"
            android:maxLines="1"
            android:singleLine="true"
            app:layout_constraintStart_toEndOf="@id/txt"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </android.support.constraint.ConstraintLayout>

</LinearLayout>

您的问题是错误def rom2dec(char): for s in (('I', 1), ('V', 5), ('X', 10), ('L', 50), ('C', 100), ('D', 500), ('M', 1000)): if char == s[0]: return s[1] total=0 prev=0 romannum = input('Roman Numeral: ').upper() for s in reversed(romannum): i = rom2dec(s) if i < prev: total -= i else: total += i prev = i print(total) 表示您正在尝试访问字符索引 不在字符串长度的范围内。 字符串基于0,因此第一个字符位于索引0。

使用string index out of range时,需要确保 romannum[i + 1]不大于所存储字符串的长度 在名为i + 1的变量中减去1,因为它是基于0的。

罗马数字趋向于求反 即romannum之前的I被解释为4,因为V小于 I并在V之前。

因此,如果字符串反转,则将每个字符更改为 十进制等效项,然后与最后存储的十进制进行比较 确定是要添加还是减去当前十进制数字的值。

答案 3 :(得分:0)

这个概念真的很有趣,这里有一个不同的观点,如果您想添加更多的数字,例如LCDM,加油!

while True:
    numeral = input("Enter a Roman Numeral: ")

    numeral = list(numeral.lower())

    final = 0 

    for index, items in enumerate(numeral):

        if items == 'x':
            if index == 0:
                final += 10
            elif numeral[index - 1] != 'i':
                final += 10
            else:
                final += 9

        elif items == 'v':
            if index == 0:
                final += 5
            elif numeral[index - 1] != 'i':
                final += 5
            else:
                final += 4    

        elif items == 'i':
            if index == len(numeral)-1: 
                final += 1
            elif numeral[index + 1] != 'v' and numeral[index + 1] != 'x':
                final += 1
            else:
                pass

    print(final)