python求和函数详解_关于python:对多个数字求和的函数

本问题已经有最佳答案,请猛点这里访问。

我刚接触到Python,开始学习执行函数。我开始添加数字,但我只能求和两个数字,如果我想求和更多,就需要编辑程序。这是我的密码

1

2

3

4

5

6

7def sum(num1,num2):

return num1+num2

a=5

b=7

c=sum(a,b)

print (c)

现在,我想创建一个函数来求和任意数量的数字,而不需要编辑代码。我是这样做的:

1

2

3

4

5

6

7

8

9

10

11

12def sum(num1,num2):

return num1+num2

a=int(input("what is the number you want to add?:"))

ask="yes"

while(ask=="yes"):

b=int(input("what is the number you want to add?:"))

c=sum(a,b)

a=c

ask=input("do you want to add another number?:")

else:

c=a

print (c)

这是可行的,但我认为应该有一个更简单的方法来实现这个功能…正确的?谢谢你的帮助!

您希望在函数中有一个*args参数,这样您可以接受多于1个的输入:

1

2

3

4

5def summ(num1, *args):

total = num1

for num in args:

total = total + num

return total

*args的意思是,你可以根据自己的需要传递尽可能多的论点:

1

2

3

4>>> summ(1, 2, 3)

6

>>> summ(1, 2, 3, 4, 5, 6, 7)

28

args是一个迭代对象:我们遍历它,并将它的每个数字相加到total中,然后返回。

total = total + num=>total += num。

@Jean-Fran&231;Oisfabre Sure:P.我一直喜欢total = total + num的清晰。

那么,为什么不是return num1 + sum(args)?

@Jean-Fran&231;Oisfabre我认为这部作品学习的是写作功能,而不是内置的东西。使用sum()会破坏整个问题的目的!

对于列表,由于二次效应,不要使用list1 = list1 + list2。但是对于整数来说应该是无痛的。

@Jean Fran&231;Oisfabre是真的,是的。但这正是list.extend()的用武之地;)

不管您喜欢什么,不要在有意义的地方使用增广赋值,它闻起来像初学者代码(它还可能阻止一些优化按预期工作)。

可以使用变量参数接受任意数量的参数

1

2

3

4

5def my_sum(*args):

total = 0

for val in args:

total += val

return total

您还可以使用python的内置sum()添加它们:

1

2

3

4

5

6

7def my_sum(*args):

return sum(args)

>>> my_sum(1,2,3,4)

10

>>> my_sum(1,5,6,7.8)

19.8

首先要注意的是,python有一个本机sum函数。用这个代替简单的计算,不要用你自己的覆盖它。

但是要了解更多关于python的信息,您可能希望使用functools.reduce,它将两个参数的函数累计应用于序列的项。

1

2

3

4

5

6

7

8from functools import reduce

def mysum(*nums):

return reduce(lambda x, y: x+y, nums)

a, b, c = 1, 2, 3

res = mysum(a,b,c) # 6

通过构建一个列表,然后为您的函数提供信息,可以将其合并到您的逻辑中:

1

2

3

4

5

6

7

8

9

10

11

12lst = []

lst.append(int(input("what is the number you want to add?:")))

ask ="yes"

while ask =="yes":

lst.append(int(input("what is the number you want to add?:")))

ask = input("do you want to add another number?:")

else:

res = mysum(*lst)

print(res)

为什么只使用模块来添加元组元素?!sum()函数可在此处使用。

@Eiram_Mahera,因为这个用户想了解Python。这就是为什么。

我建议您根据以下解释进行更新:

我建议不要为用户定义的函数使用名称sum,因为这是python内置函数的名称,即使它在您的情况下还没有造成任何问题。

可以使用sum内置函数对列表中的元素求和。

在while之后的else是没有用的。

您可以使用a = sum([a,b])直接分配a,而不使用临时变量c。

最后,这里是我对代码的建议:

1

2

3

4

5

6

7

8

9a = int(input("what is the number you want to add?:"))

ask ="yes"

while (ask =="yes"):

b = int(input("what is the number you want to add?:"))

a = sum([a,b])

ask = input("do you want to add another number?:")

print (a)

sum不是"保留的内置符号",它只是一个内置函数。如果它是保留名称,尝试将其用作标识符会引发语法错误。

@你说得对,先生!我已经更新了我的答案。谢谢。

有几种方法可以添加不同数量的数字。首先,您可以使用列表和内置函数和:埃多克斯1〔6〕

如果不想使用列表,请尝试在内置和函数上编写自定义包装(最好不要重写内置名称):

1

2

3

4def my_sum(*args):

return sum(args)

my_sum(1, 2, 3)

此外,这种用法是可能的:

1

2my_sum(2) # result is 2

my_sum() # result is 0

但是,如果您想要一个函数,它至少需要两个参数,请尝试以下操作:

1

2

3

4def my_sum(num1, num2, *args):

return num1 + num2 + sum(args)

my_sum(1, 2, 3)

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值