Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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 3.x 将罗马数字转换为国际系统的简单Python程序_Python 3.x - Fatal编程技术网

Python 3.x 将罗马数字转换为国际系统的简单Python程序

Python 3.x 将罗马数字转换为国际系统的简单Python程序,python-3.x,Python 3.x,我试图编写一个基本程序,将罗马数字转换为传统数字,但我似乎犯了一些错误,现在这是一个成功或失败的结果。请看一下代码。我在代码中更好地将其描述为注释。谢谢 def roman(roman_num): inter_dic = { "I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000 } output

我试图编写一个基本程序,将罗马数字转换为传统数字,但我似乎犯了一些错误,现在这是一个成功或失败的结果。请看一下代码。我在代码中更好地将其描述为注释。谢谢

def roman(roman_num):
inter_dic = {
    "I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000
    }
output = 0
num = roman_num.upper()
#this is to ensure that the number appears as uppercase alphabets

for numeral in num:
    numeral_index = num.index(numeral)
#I've squeezed an exception here to handle the last digit in the number
    try:
        #for the additive part
        if inter_dic[numeral] > inter_dic[num[numeral_index + 1]]:
            output += inter_dic[numeral]
        #for the subtraction part
        elif inter_dic[numeral] < inter_dic[num[numeral_index + 1]]:
            output -= inter_dic[numeral]
        
        elif inter_dic[numeral] == inter_dic[num[numeral_index + 1]]:
            output += inter_dic[numeral]
        #the following line is actually dead code,but I've added it just in case.
        else:
            print("There was an error.")
    #the IndexError will be called when the last digit is reached and so the last digit 
    #will be added

    except IndexError:
        output += inter_dic[numeral]
          
return output


assert roman("cxcix") == 199
#this returns an assertion error
#when the function is called,the output is 179
def roman(罗马字母):
内部dic={
“I”:1,“V”:5,“X”:10,“L”:50,“C”:100,“D”:500,“M”:1000
}
输出=0
num=roman_num.upper()
#这是为了确保数字显示为大写字母
对于num中的数字:
数字索引=数字索引(数字)
#我在这里压缩了一个例外来处理数字中的最后一个数字
尝试:
#对于附加部分
如果inter_dic[数字]>inter_dic[数字索引+1]:
输出+=内部dic[数字]
#减法部分
elif inter_dic[数字]
这应该满足您的要求:

def roman(roman_num):
    inter_dic = {
        "I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000
        }
    x = list(map(lambda x: inter_dic[x], roman_num.upper()))
    for idx in range(len(x)-1):
        if x[idx+1] > x[idx]:
            x[idx] *= -1
    return x

decimal = roman("cxcix")
print(decimal) # Output: [100, -10, 100, -1, 10]
print(sum(decimal)) # Output: 199
这是基于数字结构合理的假设。如中所示,所代表的数字应按从大到小的顺序排列


即使只给它一个字符,上面的代码也可以工作,因为循环是基于给定列表的
范围(len()-1)
,该列表是在我们将字母转换为整数时创建的

如果
x==[100]
那么
len(x)-1==0
将立即终止for循环,因此我们不会在循环内部遇到任何
索引器


为了解释代码中与我的版本不同的地方,我们可以在这里创建一个简单的示例:

lst = list("abcabc")
for idx, letter in enumerate(lst):
    print(f"Letter: {letter}, list.index: {lst.index(letter)}, Actual index: {idx}")
输出:

Letter: a, list.index: 0, Actual index: 0
Letter: b, list.index: 1, Actual index: 1
Letter: c, list.index: 2, Actual index: 2
Letter: a, list.index: 0, Actual index: 3
Letter: b, list.index: 1, Actual index: 4
Letter: c, list.index: 2, Actual index: 5
如果我们查看
list.index
的文档,我们可以看到以下描述:

index(self, value, start=0, stop=2147483647, /)
    Return first index of value.

    Raises ValueError if the value is not present.

因此,当我们调用lst检查其索引时,
lst中有重复的值,因此它只返回与给定变量匹配的第一个值。

这应该满足您的要求:

def roman(roman_num):
    inter_dic = {
        "I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000
        }
    x = list(map(lambda x: inter_dic[x], roman_num.upper()))
    for idx in range(len(x)-1):
        if x[idx+1] > x[idx]:
            x[idx] *= -1
    return x

decimal = roman("cxcix")
print(decimal) # Output: [100, -10, 100, -1, 10]
print(sum(decimal)) # Output: 199
这是基于数字结构合理的假设。如中所示,所代表的数字应按从大到小的顺序排列


即使只给它一个字符,上面的代码也可以工作,因为循环是基于给定列表的
范围(len()-1)
,该列表是在我们将字母转换为整数时创建的

如果
x==[100]
那么
len(x)-1==0
将立即终止for循环,因此我们不会在循环内部遇到任何
索引器


为了解释代码中与我的版本不同的地方,我们可以在这里创建一个简单的示例:

lst = list("abcabc")
for idx, letter in enumerate(lst):
    print(f"Letter: {letter}, list.index: {lst.index(letter)}, Actual index: {idx}")
输出:

Letter: a, list.index: 0, Actual index: 0
Letter: b, list.index: 1, Actual index: 1
Letter: c, list.index: 2, Actual index: 2
Letter: a, list.index: 0, Actual index: 3
Letter: b, list.index: 1, Actual index: 4
Letter: c, list.index: 2, Actual index: 5
如果我们查看
list.index
的文档,我们可以看到以下描述:

index(self, value, start=0, stop=2147483647, /)
    Return first index of value.

    Raises ValueError if the value is not present.
因此,因为当我们调用lst检查其索引时,它内部有重复的值,所以它只返回与我们给它的变量匹配的第一个值