Reputation: 43
Numbers that do not contain 4 convert just fine, but once number that contain 4 is tested, it does not convert properly.
I am new to python and I am struggling to see what was wrong in the code. The code for converting Arabic number to Roman numerals work for numbers that does not contain 4 in them. I have tried to test with different combination of numbers. The codes before the one below pretty much determine how many thousands, five hundreds, hundreds, etc that is in the number inputted. Could anyone help me?
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)
If I input numbers like 4 or 14, I expect to get IV and XIV respectively. But the actual outputs are IIII and XIIII respectively.
Please help. I'm sorry if there is something wrong with the format of my question as I am also new to stackoverflow. Thank you in advance.
Upvotes: 1
Views: 5219
Reputation: 11
Done in 2 lines! (Has an upper limit of 4000) Here's the code:
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])
Upvotes: 1
Reputation: 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")
Upvotes: -1
Reputation: 4775
This converts any positive integer to roman numeral string:
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))]))
Test code:
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))
Output:
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
Note that integers greater than 9 million are represented by the characters that contains 2 or more macrons, which are very unclear unless they are badly scaled-up
Upvotes: 1
Reputation: 898
Welcome to SO! The problem is the way you are trying to define and change your variables. For example, this piece of code:
elif I == 4:
I == 0
IV == 1
should look like this instead:
elif I == 4:
I = 0
IV = 1
==
is a boolean Operator that will return True
if two values are the same and False
if they are not. =
is the correct way to assign a new value to a variable. After changing this, all works as intended.
display_roman(0, 0, 0, 0, 0, 0, 4)
display_roman(0, 0, 0, 0, 0, 1, 4)
The roman numeral of your number is:
IV
The roman numeral of your number is:
IX
Upvotes: 3