我如何告诉 Python 将整数转换为单词 [英] How do I tell Python to convert integers into words

查看:46
本文介绍了我如何告诉 Python 将整数转换为单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图告诉 Python 将整数转换为单词.

I'm trying to tell Python to convert integers into words.

示例:(使用墙上的歌曲 99 瓶啤酒)

Example: (using the song 99 bottles of beer on the wall)

我用这段代码来编写程序:

I used this code to write the program:

for i in range(99,0,-1):
    print i, "Bottles of beer on the wall,"
    print i, "bottles of beer."
    print "Take one down and pass it around,"
    print i-1, "bottles of beer on the wall."
    print

但我不知道如何编写程序以便显示单词(即九十九、九十八等)而不是数字.

But I cannot figure out how to write the program so that the words (i.e. Ninety nine, Ninety eight, etc.) will be displayed instead of the numbers.

我一直在我拥有的 python 书中绞尽脑汁,我明白也许我只是不理解 for/if/elif/else 循环,但我只是在旋转我的轮子.

I have been wracking my head in the python book I have, I understand that maybe I just do not understand for/if/elif/else loops yet but I'm just spinning my wheels.

谁能提供任何见解?我不是在寻找直接的答案,尽管这可能会帮助我看到我的问题,但只要能将我指向正确的方向就好了.

Could anyone provide any insight? I'm not looking for a direct answer, although that might help me see my problem, just anything to point me in the right direction would be great.

推荐答案

inflect 包可以做到这一点.

The inflect package can do this.

https://pypi.python.org/pypi/inflect

$ pip install inflect

然后:

>>>import inflect
>>>p = inflect.engine()
>>>p.number_to_words(99)
ninety-nine

这篇关于我如何告诉 Python 将整数转换为单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆