回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我正在创建一个数字到单词的翻译,并有一个例外,它的工作。你知道吗</p>
<p>例如,如果在<code>4056</code>中键入了数字,则在<code>'four thousand zero hundred fifty six'</code>输出时,我很难计算零。此外,如果输入<code>50</code>,则输出<code>'fifty zero'</code>。你知道吗</p>
<p>到目前为止,我掌握的情况如下:</p>
<pre><code>def convert(number_str):
d1 = {0: 'zero',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'}
l2 = ['twenty','thirty','fourty','fifty','sixty','seventy','eighty','ninety']
if (int(number_str) >= 1) and (int(number_str) < 19):
return(d1[number_str])
elif (int(number_str) >= 20) and (int(number_str) <99):
tens, single = divmod(number_str, 10)
return l2[tens-2] + " " + d1[single]
elif (int(number_str) >= 100) and (int(number_str) <999):
hundreds, tens1 = divmod(number_str, 100)
tens, single = divmod(tens1,10)
return(d1[hundreds]+' hundred '+l2[tens-2]+" "+d1[single])
elif (int(number_str) >= 1000) and (int(number_str) <9999):
thousands, hundreds1 = divmod(number_str,1000)
hundreds, tens1 = divmod(hundreds1,100)
tens, single = divmod(tens1,10)
return(d1[thousands]+' thousand '+d1[hundreds]+' hundred '+l2[tens-2]+" "+d1[single])
def main():
user_input = input('> ')
while user_input != 'quit':
print(convert(int(user_input)))
user_input = input('> ')
main()
</code></pre>
<p>为了更好地处理使用零来支持这个程序,我可以添加什么逻辑?提前谢谢!你知道吗</p>