简体   繁体   English

如何在函数参数中传递多个值?

[英]How can I pass multiple values in a function parameter?

I am trying to make a program that calculates compound interest with 3 different lists. 我正在尝试制作一个可以计算3个不同列表的复利的程序。 The first item in each list are the variables needed in the formula A=P(1+r)^n. 每个列表中的第一项是公式A = P(1 + r)^ n中需要的变量。 These are the instructions. 这些是说明。

Albert Einstein once said “compound interest” is man’s greatest invention. Use the equation A=P(1+r) n
,
where P is the amount invested, r is the annual percentage rate (as a decimal 5.0%=0.050) and n is the
number of years of the investment.
Input: 3 lists representing investments, rates, and terms
investment = [10000.00, 10000.00, 10000.00, 10000.00, 1.00]
rate = [5.0, 5.0, 10.0, 10.0, 50.00]
term = [20, 40, 20, 40, 40]
Output: Show the final investment.
$26532.98
$70399.89
$67275.00
$452592.56
$11057332.32

This is the code that I have written so far: 这是我到目前为止编写的代码:

P = [10000.00, 10000.00, 10000.00, 10000.00, 1.00]
r = [5.0, 5.0, 10.0, 10.0, 50.00]
n = [20, 40, 20, 40, 40]

# A=P(1+r)
def formula(principal,rate,years):
    body = principal*pow((1 + rate),years)
    print "%.2f" %(body)
def sort(lst):
    spot = 0
    for item in lst:
        item /= 100
        lst[spot] = item
        spot += 1

input = map(list,zip(P,r,n))
sort(r)
for i in input:
    for j in i:
        formula()

I firstly define a function to calculate the compound interest, then I define a function to convert the rates to the proper format. 我首先定义一个计算复利的函数,然后定义一个将利率转换为正确格式的函数。 Then using map (which im not completely familiar with)I separate the first item of each list into a tuple within the new input list. 然后使用map(我并不完全熟悉),我将每个列表的第一项分成新输入列表中的元组。 What i am trying to do is find a way to input the three items within in the tuple into principle, rate and years in the formula function. 我想做的是找到一种方法,可以将元组中的三个项目输入到公式函数中的原理,比率和年份。 I am open to critiquing, and advice. 我愿意提出批评和建议。 I am still fairly new with programming in general. 一般来说,我对编程还是比较陌生的。 Thank you. 谢谢。

Firstly, I think you should return something from your formula , that is, your calculation: 首先,我认为您应该从formula return一些内容,即您的计算结果:

def formula(principal,rate,years):
    return principal*pow((1 + rate),years) #return this result

then you can use the return value from the formula - be it for printing or any other use for further calculation. 那么您可以使用formulareturn值-用于打印还是用于其他计算。

Also, since you have the same amount of items in your three lists, why not just using range(len(p)) to iterate over them? 另外,由于三个列表中的项目数量相同,为什么不只使用range(len(p))遍历它们呢?

for x in range(len(p)):
    print(formula(p[x],r[x],n[x]))

x in range(len(p)) will generate iteration with x value: x in range(len(p)) x将生成具有x值的迭代:

0, 1, ..., len(p) - 1 # in your case, len(p) = 5, thus x ranges from 0 to 4

And p[x] is the way you say you want to get x-th-indexed element from p . p[x]是您要从p获得第x-th-indexed元素的方式。 Putting it you your context you will get combination like this: 把它放在您的上下文中,您将获得如下组合:

when x=   principal   rate   years
----------------------------------
  0       10000.00     5.0     20
  1       10000.00     5.0     40
  2       10000.00    10.0     20
  3       10000.00    10.0     40
  4           1.00    50.0     40

This way, you do not need to use tuple 这样,您不需要使用tuple

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将对象属性作为函数参数传递给lambda函数? - How can I pass an object attribute to lambda function, as function parameter? 如何将多个用户输入值从 PySimpleGUI 传递给函数,然后将结果输出到 PySimpleGUI? - How can I pass multiple user input values from PySimpleGUI to a function, then output result to PySimpleGUI? 当只有一些变量需要多个变量时,如何将多个值传递给函数-Python 3 - How can I pass multiple values to a function when only some variables need more than one - python 3 如何在没有“lambda”关键字的情况下将方法作为函数参数传递? - How can I pass method as function parameter without 'lambda' keyword? python - 如何在python pandas管道中将函数作为参数传递给参数 - How can I pass function as argument with parameter in python pandas pipe 如何将参数传递给Python vegas库中调用的函数? - How can I pass parameter to a function called in Python vegas library? 如何将字典列表作为函数参数传递? - how can I pass a list of dictionaries as function parameter? 如何将 function 参数作为 python 中的模块属性传递? - How can I pass a function parameter as an attribute of a module in python? 如何将jinja2对象传递给jquery函数作为参数? - How can I pass a jinja2 object to a jquery function as parameter? 如何在函数中传递namedtuple的可选参数 - How can I pass the optional parameter of a namedtuple in a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM