t = {'M' : 1000, 'D' : 500, 'C' : 100, 'L' : 50, 'X' : 10, 'V' : 5, 'I' : 1
, 'IV' : 4, 'IX' : 9, 'XL' : 40, 'XC' : 90, 'CD' : 400, 'CM' : 900}
b = {1000 : 'M', 900 : 'CM', 500 : 'D', 400 : 'CD', 100 : 'C', 90 : 'XC', 50 : 'L'
, 40 : 'XL', 10 : 'X', 9 : 'IX', 5 : 'V', 4 : 'IV', 1 : 'I'}
def turn_num_other(n):
list1 = [t[c] for c in n]
for i in range(len(n)-1):
if list1[i] < list1[i+1]:
list1[i] *= -1
ans2 = sum(list1)
return ans2
def turnchar(x):
l = ''
d = 0
while d == 0:
if x >= 0:
for key in b:
if x >= key:
x -= key
l += b[key]
break
else:
continue
if x == 0:
d = 1
return l
a, b = input().split()
while a != '#':
a1 = turn_num_other(a)
b1 = turn_num_other(b)
if a1 > b1:
k = a1 - b1
print(k)
print(type(k))
final1 = turnchar(k)
print(final1)
elif a1 == b1:
print('ZERO')
if a1 < b1:
k = b1 - a1
print(k)
print(turnchar(k))
a, b = input().split()
他是說在 if x >= key 有錯 不過兩個的type都是int為何會有這錯誤?
t = {'M' : 1000, 'D' : 500, 'C' : 100, 'L' : 50, 'X' : 10, 'V' : 5, 'I' : 1
, 'IV' : 4, 'IX' : 9, 'XL' : 40, 'XC' : 90, 'CD' : 400, 'CM' : 900}
b = {1000 : 'M', 900 : 'CM', 500 : 'D', 400 : 'CD', 100 : 'C', 90 : 'XC', 50 : 'L'
, 40 : 'XL', 10 : 'X', 9 : 'IX', 5 : 'V', 4 : 'IV', 1 : 'I'}
def turn_num_other(n):
list1 = [t[c] for c in n]
for i in range(len(n)-1):
if list1[i] < list1[i+1]:
list1[i] *= -1
ans2 = sum(list1)
return ans2
def turnchar(x):
l = ''
d = 0
while d == 0:
if x >= 0:
for key in b:
if x >= key:
x -= key
l += b[key]
break
else:
continue
if x == 0:
d = 1
return l
a, b = input().split()
while a != '#':
a1 = turn_num_other(a)
b1 = turn_num_other(b)
if a1 > b1:
k = a1 - b1
print(k)
print(type(k))
final1 = turnchar(k)
print(final1)
elif a1 == b1:
print('ZERO')
if a1 < b1:
k = b1 - a1
print(k)
print(turnchar(k))
a, b = input().split()
他是說在 if x >= key 有錯 不過兩個的type都是int為何會有這錯誤?
不是啊,key的type是str,因為b是str,是輸入的第二個羅馬數字
t = {'M' : 1000, 'D' : 500, 'C' : 100, 'L' : 50, 'X' : 10, 'V' : 5, 'I' : 1
, 'IV' : 4, 'IX' : 9, 'XL' : 40, 'XC' : 90, 'CD' : 400, 'CM' : 900}
b = {1000 : 'M', 900 : 'CM', 500 : 'D', 400 : 'CD', 100 : 'C', 90 : 'XC', 50 : 'L'
, 40 : 'XL', 10 : 'X', 9 : 'IX', 5 : 'V', 4 : 'IV', 1 : 'I'}
def turn_num_other(n):
list1 = [t[c] for c in n]
for i in range(len(n)-1):
if list1[i] < list1[i+1]:
list1[i] *= -1
ans2 = sum(list1)
return ans2
def turnchar(x):
l = ''
d = 0
while d == 0:
if x >= 0:
for key in b:
if x >= key:
x -= key
l += b[key]
break
else:
continue
if x == 0:
d = 1
return l
a, b = input().split()
while a != '#':
a1 = turn_num_other(a)
b1 = turn_num_other(b)
if a1 > b1:
k = a1 - b1
print(k)
print(type(k))
final1 = turnchar(k)
print(final1)
elif a1 == b1:
print('ZERO')
if a1 < b1:
k = b1 - a1
print(k)
print(turnchar(k))
a, b = input().split()
他是說在 if x >= key 有錯 不過兩個的type都是int為何會有這錯誤?
不是啊,key的type是str,因為b是str,是輸入的第二個羅馬數字
我發現錯誤了 我上面的DICT命名也是b,我把input的變數名稱改成C就沒問題了
感謝~