不包含4的数字可以很好地转换,但是一旦包含4的数字经过测试,就不能正确转换。
我是python的新手,我正努力查看代码中的错误。用于将阿拉伯数字转换为罗马数字的代码适用于其中不包含4的数字。我尝试用不同的数字组合进行测试。下面的代码之前的代码几乎确定了输入的数字是几千,五百,几百等等。有人可以帮我吗?
def display_roman(M, D, C, L, X, V, I):
CM = 0
CD = 0
XC = 0
XL = 0
IX = 0
IV = 0
if D == 2:
M += 1
D -= 2
elif L == 2:
C += 1
L -= 2
elif V == 2:
X += 1
V -= 2
if V == 1 and I == 4:
V = 0
I = 0
IX = 1
elif I == 4:
I == 0
IV == 1
if X == 4:
X == 0
XL == 1
if L == 1 and X == 4:
L == 0
X == 0
XC == 1
if C == 4:
C == 0
CD == 1
if D == 1 and C == 4:
D == 0
C == 0
CM == 1
print("The roman numeral of your number is: ")
print("M" * M, "CM" * CM, "D" * D, "CD" * CD, "C" * C,"XC" * XC, "L" * L, "XL" * XL, "X" * X, "IX" * IX, "V" * V, "IV" * IV, "I" * I)
如果我输入数字4或14,我希望分别得到IV和XIV。但是实际产出分别是IIII和XIIII。
请帮助。如果问题的格式出现问题,我感到很抱歉,因为我也是stackoverflow的新手。预先谢谢你。
答案 0 :(得分:3)
欢迎您! 问题是您尝试定义和更改变量的方式。例如,这段代码:
REFERENCES
应该看起来像这样:
elif I == 4:
I == 0
IV == 1
elif I == 4:
I = 0
IV = 1
是一个布尔运算符,如果两个值相同,则返回==
,如果两个值不相同,则返回True
。 False
是将新值分配给变量的正确方法。更改此设置后,所有功能均按预期工作。
=
答案 1 :(得分:0)
这会将任何正整数转换为罗马数字字符串:
def roman(num: int) -> str:
chlist = "VXLCDM"
rev = [int(ch) for ch in reversed(str(num))]
chlist = ["I"] + [chlist[i % len(chlist)] + "\u0304" * (i // len(chlist))
for i in range(0, len(rev) * 2)]
def period(p: int, ten: str, five: str, one: str) -> str:
if p == 9:
return one + ten
elif p >= 5:
return five + one * (p - 5)
elif p == 4:
return one + five
else:
return one * p
return "".join(reversed([period(rev[i], chlist[i * 2 + 2], chlist[i * 2 + 1], chlist[i * 2])
for i in range(0, len(rev))]))
测试代码:
print(roman(6))
print(roman(78))
print(roman(901))
print(roman(2345))
print(roman(67890))
print(roman(123456))
print(roman(7890123))
print(roman(45678901))
print(roman(234567890))
输出:
VI
LXXVIII
CMI
MMCCCXLV
L̄X̄V̄MMDCCCXC
C̄X̄X̄MMMCDLVI
V̄̄M̄M̄D̄C̄C̄C̄X̄C̄CXXIII
X̄̄L̄̄V̄̄D̄C̄L̄X̄X̄V̄MMMCMI
C̄̄C̄̄X̄̄X̄̄X̄̄M̄V̄̄D̄L̄X̄V̄MMDCCCXC
请注意,大于9百万的整数是由包含2个或多个宏元的字符表示的,除非它们的缩放比例不正确,否则它们非常不清楚
答案 2 :(得分:0)
两行完成! (上限为 4000) 代码如下:
n,k = {0:'',1:'I',2:'II',3:'III',4:'IV',5:'V',6:'VI',7:'VII',8:'VIII',9:'IX',10:'X',20:'XX',30:'XXX',40:'XL',50:'L',60:'LX',70:'LXX',80:'LXXX',90:'XC',100:'C',200:'CC',300:'CCC',400:'CD',500:'D',600:'DC',700:'DCC',800:'DCCC',900:'CM',1000:'M',2000:'MM',3000:'MMM',4000:'MMMM'},int(input('ARABIC TO ROMAN CONVERTER:-\n\n>> Enter Arabic Number: '))
print('>> Arabic Equivalent:',n[(k//1000)*1000] + n[((k-((k//1000)*1000))//100)*100] + n[((k-(((k-((k//1000)*1000))//100)*100)-(k//1000)*1000)//10)*10] + n[k%10])
答案 3 :(得分:-1)
print("ARABIC TO ROMAN CONVERTER [1-3999]:- \n \n")
x=int(input("ENTER THE ARABIC NUMBER: "))
b=["",'I','II','III','IV','V','VI','VII','VIII','IX','X','XX','XXX','XL','L','LX','LXX','LXXX','XC','C','CX','CXX','CXXX','CXL','CL','CLX','CLXX','CLXXX','CXC','CC','CCC','CD','D','DC','DCC','DCCC','CM','M']
d=["",'X','XX','XXX','XL','L','LX','LXX','LXXX','XC']
e=["",'C','CC','CCC','CD','D','DC','DCC','DCCC','CM']
if x in range(1,1000):
print(e[int((((int(x/100))*100)-1000)/100)]+d[int((x-(int(x/100)*100))/10)]+b[((x%1000)%100)%10])
if x==1000:
print(b[37])
if x in range(1001,2000):
print(b[37]+e[int((((int(x/100))*100)-1000)/100)]+d[int((x-(int(x/100)*100))/10)]+b[((x%1000)%100)%10])
if x==2000:
print("MM")
if x in range(2001,3000):
print(b[37]+b[37]+e[int((((int(x/100))*100)-2000)/100)]+d[int((x-(int(x/100)*100))/10)]+b[((x%1000)%100)%10])
if x==3000:
print("MMM")
if x in range(3001,4000):
print(b[37]+b[37]+b[37]+e[int((((int(x/100))*100)-3000)/100)]+d[int((x-(int(x/100)*100))/10)]+b[((x%1000)%100)%10])
if x not in range(1,4000):
print("Error")