dict1={"I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000}dict2=dict(zip(dict1.values(),dict1.keys()))appear=""while True:number=input("").split(" ")if number == ["#"]:breakn1=list(number[0])n2=list(number[1])add1=0add2=0for i in range (len(n1)-1):if dict1[n1[i]]<dict1[n1[i+1]]:add1-=dict1[n1[i]]else:add1+=dict1[n1[i]]add1+=dict1[n1[len(n1)-1]]for i in range (len(n2)-1):if dict1[n2[i]]<dict1[n2[i+1]]:add2-=dict1[n2[i]]else:add2+=dict1[n2[i]]add2+=dict1[n2[len(n2)-1]]if add1>=add2:add3=add1-add2else:add3=add2-add1if add3==0:print("ZERO")else:time=0while True:if add3>=1000:time=add3//1000add3=add3%1000else:breakwhile True:if time>0:time-=1appear+="M"else:breaktime=0while True:if add3>=100:time=add3//100add3=add3%100else:breakwhile True:if time ==4:appear+="CD"time-=4if time == 9:appear+="CM"time-=9if time>=5:time-=5appear+="D"if time>0:time-=1appear+="C"else:breaktime=0while True:if add3>=10:time=add3//10add3=add3%10else:breakwhile True:if time ==4:appear+="XL"time-=4if time == 9:appear+="XC"time-=9if time>=5:time-=5appear+="C"if time>0:time-=1appear+="X"else:breakwhile True:if add3 ==4:appear+="IV"add3-=4if add3 == 9:appear+="IX"add3-=9if add3>=5:add3-=5appear+="V"if add3>0:add3-=1appear+="I"else:breakprint(appear)
1. 你的appear沒有在每個測資間清空,會變得越來越長。你可以把appear=0放在while迴圈裡面
2.
這裡應該是appear+="L"