Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 罗马整数到整数-简单_Python_Python 3.x_For Loop_Roman Numerals - Fatal编程技术网

Python 罗马整数到整数-简单

Python 罗马整数到整数-简单,python,python-3.x,for-loop,roman-numerals,Python,Python 3.x,For Loop,Roman Numerals,赋值要求将罗马整数转换为整数。但是,如果没有与罗马整数相关的语法(即IV!=4 IV=6),我将尝试使我的代码更高效 这是我的原始代码: def roman_v2(): s = input("Enter a roman number using capital letters M, D, C, X, V and I: ") count = 0 r_nums = ["M","D","C","X","V","I"] for letter in s: i

赋值要求将罗马整数转换为整数。但是,如果没有与罗马整数相关的语法(即IV!=4 IV=6),我将尝试使我的代码更高效

这是我的原始代码:

def roman_v2():
    s = input("Enter a roman number using capital letters M, D, C, X, V and I: ")
    count = 0
    r_nums = ["M","D","C","X","V","I"]
    for letter in s:
        if letter in r_nums[0]:
            count += 1000
        elif letter in r_nums[1]:
            count += 500
        elif letter in r_nums[2]:
            count += 100
        elif letter in r_nums[3]:
            count += 10
        elif letter in r_nums[4]:
            count += 5
        elif letter in r_nums[5]:
            count += 1    
    return count
这是我的程序错误:

def roman_v2():
    s = input("Enter a roman number using capital letters M, D, C, X, V and I: ")
    count = 0
    r_nums = [["M",1000],["D",500],["C",100],["X",10],["V",5],["I",1]]
    for i in s:
        for j in range(0,5):
            if s[i] == r_nums[j][0]:
                count += r_nums[i][1]
    return count

您可以使用字典和
sum()
函数收紧代码:

def roman_v2():
    s = input("Enter a roman number using capital letters M, D, C, X, V and I: ")
    numbers = {'M':1000, 'D':500, 'C':100, 'X':10, 'V':5, 'I':1}
    return sum(numbers[num] for num in s)
这将查找每个字母的值,将它们全部相加,然后返回总数。错误检查(针对错误输入,如
'MMpotato'
)由您决定。

这一点

if s[i] == r_nums[j][0]:
应该是

if i == r_nums[j][0]:
因为我将是角色,而不是索引

count+=r\u nums[i][1]
中,i是错误的,应该是j

给你

def roman_v2():
    s = input("Enter a roman number using capital letters M, D, C, X, V and I: ")
    count = 0
    r_nums = [["M",1000],["D",500],["C",100],["X",10],["V",5],["I",1]]
    for i in s:
        for j in range(0,5):
            if i == r_nums[j][0]:
                count += r_nums[j][1]
    return count

您可以在python中使用
dict

def roman_v2():
    s = s = input("Enter a roman number using capital letters M, D, C, X, V and I: ")
    r_nums = {'M': 1000, 'D': 500, 'C': 100, 'X': 10, 'V': 5, 'I': 1}
    count = 0
    for i in s:
        count += r_nums.get(i)
    return count

IV
do等于
4
我知道,但为了完成作业,我们需要做的就是I+V==1+5==6请发布您的错误。我觉得听写更合适。此外,您的缩进已关闭。如果int(s[i])==int(r_nums[j][0]):类型错误:字符串索引必须是整数,我在实际程序中上载错误缩进正确:)@cdonts根据OP
IV
应该是
6
。看看下面的评论question@cdonts-应该是这样的。我的修改还是有错误?count+=r_nums[i][1]TypeError:列表索引必须是整数,而不是strI我刚才在检查这个时看到了!!非常感谢你!谢谢@sam2090,看到一个错误,没有检查其他错误。