開發人員您好,我是 Python 新手,我只想將數字轉換為羅馬字符,我知道已經有了,但我不明白如何在我的代碼中執行此操作:
t = int(input("repeat : "))
for i in range(t):
try:
n = int(input(""))
except ValueError:
print("incorrect input")
romnumber = { 1: 'I', 4: 'IV', 5: 'V', 9: 'IX', 10: 'X', 40: 'XL',
50: 'L', 90: 'XC', 100: 'C', 400: 'XD', 500: 'D', 900: 'CM', 1000: 'M'}
print_order = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
for x in print_order:
if n != 0:
quotient= n//x
#If quotient is not zero output the roman equivalent
if quotient != 0:
for y in range(quotient):
print(romnumber[x], end="")
#update integer with remainder
n = n%x
我想輸入是這樣的:
repeat: 2
1
3
和輸出:
I
III
但現在它只保存最后一個輸入不知道如何獲得 2 個或更多輸入以將數字轉換為羅馬數字
uj5u.com熱心網友回復:
您需要在回圈內執行操作,或者保存輸入:
t = int(input("repeat : "))
romnumber = { 1: 'I', 4: 'IV', 5: 'V', 9: 'IX', 10: 'X', 40: 'XL',
50: 'L', 90: 'XC', 100: 'C', 400: 'XD', 500: 'D', 900: 'CM', 1000: 'M'}
print_order = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
inputs = []
for i in range(t):
try:
inputs.append(int(input("")))
except ValueError:
print("incorrect input")
for n in inputs:
for x in print_order:
if n != 0:
quotient= n//x
#If quotient is not zero output the roman equivalent
if quotient != 0:
for y in range(quotient):
print(romnumber[x], end="")
#update integer with remainder
n = n%x
print()
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/364711.html
上一篇:為什么xlsxwriter不更新Pandas資料框?
下一篇:根據最近的記錄加入pyspark
