首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我如何编写一个for循环来测试我所有的5个测试用例?

我如何编写一个for循环来测试我所有的5个测试用例?
EN

Stack Overflow用户
提问于 2021-08-14 03:08:47
回答 1查看 69关注 0票数 1

在给定目标财富的情况下,我应该计算出定期投资额。

这是我的用户定义代码:

代码语言:javascript
复制
def contribution_calculator(target_wealth, rate, duration, periodicity):
    inv_amt = -npf.pmt(rate/100/periodicity, duration*periodicity, 0, target_wealth)
    return inv_amt

这是我的5个测试用例,我已经将它们放到了各自的列表中。

代码语言:javascript
复制
target_wealth = [1000000, 1000000, 3000000, 3000000, 2000000]

rate = [2.5, 2.5, 0.5, 4.0, 3.0]

duration = [25, 12, 25, 25, 10]

periodicity = [12, 1, 12, 12, 2]

例如,测试用例1的值是1000000,2.5,25,12。

我如何编写一个for循环来测试所有5个给定的测试用例?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-14 03:15:43

您可以使用zip()和元组解包,如下所示:

代码语言:javascript
复制
for tw, r, d, p in zip(target_wealth, rate, duration, periodicity):
    ...

也许可以重命名列表,这样您就可以使用完整的变量名:

代码语言:javascript
复制
for target_wealth, rate, duration, periodicity in zip(target_wealths, rates, durations, periodicities):
    ...

PS:如果你想测试所有的组合,而不是相应的值,你可以使用itertools.product而不是zip

代码语言:javascript
复制
import itertools
for target_wealth, rate, duration, periodicity in itertools.product(target_wealths, rates, durations, periodicities):
    ...
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68780010

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档