20、參考第18題,輸入阿拉伯數字,轉成羅馬數字。
#coding=utf8
list_thos=["","M","MM","MMM","MMMM",]
list_hund=["","C","CC","CCC","CD", "D", "DC", "DCC", "DCCC", "CM",]
list_ten=["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC",]
list_one=["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX",]
print("20、輸入阿拉伯數字,轉成羅馬數字。")
print("#")
print("# M 1000 ")
print("# D 500 ")
print("# C 100 ")
print("# L 50 ")
print("# X 10 ")
print("# V 5 ")
print("# I 1 ")
while True:
x=int(input("請你輸入數字 (小於5000):"))
try:
x = int(x)
except ValueError:
print ('這是不合法的輸入. 請再輸入一次...')
continue
if ( x > 0 and x < 5000 ):
break
thos=int(x/1000)
hund=int((x/100)%10)
ten=int((x/10)%10)
one=int((x%10))
print("=============================")
print(thos,"\t",hund,"\t",ten,"\t",one)
thos1=list_thos[thos]
hund1=list_hund[hund]
ten1=list_ten[ten]
one1=list_one[one]
print("=============================")
print(thos1,"\t",hund1,"\t",ten1,"\t",one1)
print("=============================")
AllChar=thos1+hund1+ten1+one1
print("輸入數字 :",x ," 羅馬數字:",AllChar)
結果:
>>>
================= RESTART: D:/程式語言 Python 入門/50題/Ex50-20.py =================
20、輸入阿拉伯數字,轉成羅馬數字。
#
# M 1000
# D 500
# C 100
# L 50
# X 10
# V 5
# I 1
請你輸入數字 (小於5000):5698
請你輸入數字 (小於5000):457
=============================
0 4 5 7
=============================
CD L VII
=============================
輸入數字 : 457 羅馬數字: CDLVII
>>>
沒有留言:
張貼留言