【发布时间】:2019-09-06 16:33:07
【问题描述】:
我想从 for 循环中访问 range() 函数的参数值。我目前有以下工作代码:
import inflect
start = 0
stop = 5
step = 2
o = inflect.engine()
for i in range(start, stop, step):
print(f"{i} is the {o.number_to_words(o.ordinal((i + step) // step))} "
f"value when counting from {start} to {stop} by {step}")
产生以下所需的输出:
0 is the first value when counting from 0 to 5 by 2
2 is the second value when counting from 0 to 5 by 2
4 is the third value when counting from 0 to 5 by 2
我想要一种无需先定义start、stop 和step 即可生成相同输出的方法。我设想的解决方案类似于:
import inflect
o = inflect.engine()
for i in range(0, 5, 2):
start = range.getparameters[0]
stop = range.getparameters[1]
step = range.getparameters[2]
print(f"{i} is the {o.number_to_words(o.ordinal((i + step) // step))} "
f"value when counting from {start} to {stop} by {step}")
【问题讨论】:
标签: python function for-loop parameters range