Jam*_*OLA 2 python for-loop python-3.x
在下面的代码中,有没有办法可以将for循环中的'nth'更改为我正在考虑的特定术语.
例如:输入第一个术语的值,输入第二个术语的值,输入第三个术语的值,依此类推.与每次循环运行时"输入第n项的值"行相反.
for _ in range (n):
total += float (input ('Enter a value for the nth term: '))
Run Code Online (Sandbox Code Playgroud)
有一个名为Inflect的库几乎可以完成.
inflect.py - 正确生成复数,单数名词,序数,不定冠词; 将数字转换为单词.
import inflect
p = inflect.engine()
for x in range (n):
total += float (input('Enter a value for the {} term: '.format(p.ordinal(x+1))))
Run Code Online (Sandbox Code Playgroud)
放...
Enter a value for the 1st term:
Enter a value for the 2nd term:
Run Code Online (Sandbox Code Playgroud)
等等.还有一个数字到单词的功能,你应该看看 - 做同样的事情,但实际上将这个词用英语而不是"第一".