回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我写了下面的代码,假设将所有数字从1到9999转换成单词,但我正在超出范围的数字,如111等。请帮助。谢谢。在</p>
<pre><code>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)
</code></pre>