当我输入1000时,它显示我 KeyError:'1000'。这个代码中的错误是什么。这个代码有多复杂???我知道999需要0.9秒
symbols={'1':'I','2':'II','3' :'III','4': 'IV','5':' V','6': 'VI','7': 'VII','8': 'VIII','9': 'IX', '10':'X',
'40':'XL','50':'L','60':'LX','70':'LXX','80':'LXXX','90':'XC','100':'C','400':'CD','700':'DCC','800':'DCCC','900':'CM','500':'D','M':'1000'}
def get_roman(input1):
n=len(str(input1))
result=[]
while(n>=1):
unit_place=10**(int(n)-1)
div=int(input1/unit_place)
rem=input1%unit_place
result.append(div*unit_place)
input1=rem
n -=1
print(result)
此循环将打印不在符号dict
中的罗马符号for x in result:
if (str(x) not in symbols):
m=len(str(x))
tenth=10**(int(m)-1)
result_len=int(x/tenth)
roman=symbols[str(tenth)]
if(str(x)<=str(50)):
symbols[str(x)]=roman*result_len
print(symbols[str(x)],end='')
elif('49'<str(x)<'90'):
symbols[str(x)]=symbols['50']+symbols[str(x)]
print(symbols[str(x)])
elif(str(400)<str(x)<str(499)):
print(symbols['400'],symbols[str(result[1])],symbols[str(result[-1])],sep='',end='')
elif(str(499)<str(x)<str(900)):
print(symbols['500'],symbols[str(result[1])],symbols[str(result[-1])],sep='',end='')
elif(str(900)<str(x)<str(1000)):
print(symbols['1000'],symbols[str(result[1])],symbols[str(result[-1])],sep='',end='')
else:
print(symbols[str(x)],sep='-',end='')
end=time.time()
print("It took"+str(start-end)+"seconds")