我編寫了下面的代碼,它假設將所有數字從1轉換爲9999到單詞,但我的數字超出範圍111等請幫助。謝謝。將Python整數轉換爲單詞
global n2W
n2W = {1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five',\
6: 'six', 7: 'seven', 8: 'eight', 9: 'nine', 10: 'ten', \
11: 'eleven', 12: 'twelve', 13: 'thirteen', 14: 'fourteen', \
15: 'fifteen', 16: 'sixteen', 17: 'seventeen', 18: 'eighteen',\
19: 'nineteen',20:'twenty', 30:'thirty', 40:'forty', 50:'fifty', 60:'sixty',\
70: 'seventy', 80:'eighty', 90:'ninety',100:'one hundred', 200:'two hundred',\
300:'three hundred', 400:'four hundred',500:'five hundred', 600:'six hundred',\
700:'seven hundred',800:'eight hundred', 900:'nine hundred',\
1000:'one thousand', 2000:'two thousand', 3000:'three thousand',\
5000:'five thousand', 6000:'six thousand', 7000:'seven thousand',\
8000:'eight thousand', 9000:'nine thousand',10000:'ten thousand'}
def num2Word(n):
try:
print (n2W[n])
except KeyError:
try:
print (n2W[n-n%10] , n2W[n%10].lower())
except KeyError:
print ("Number out of range")
n = eval(input("Please enter a number between 1 and 9999 inclusive: "))
num2Word(n)
http://stackoverflow.com/questions/19504350/h可能的重複將數字轉換爲單詞到python – Forge