Functional arguments **args | SoloLearn: Learn to code for FREE!

0

Functional arguments **args

Given a function that takes 2 arguments and returns their sum. But we get an error when we want to sum more than 2 numbers. Change the function and complete the code so that the function sums as many numbers as are input. *args are accessible as the tuple args in the body of the function, so you can iterate through its items. def adder(x, y): print(x+y) adder(2, 3) adder(2, 3, 4) adder(1, 2, 3, 4, 5)

4/13/2021 2:25:58 AM

Veena Tirmal

7 Answers

New Answer

+8

# solution def adder(*x): print(sum(x)) adder(2, 3) adder(2, 3, 4) adder(1, 2, 3, 4, 5)

+7

P.M. It is alright. ๐Ÿ™‚๐Ÿ‘ Then I will try do give only hints in the future despite the fact that I learn best when having a working example. My deeper understanding comes by varying this example. I like to begin with a practical approach and proceed to the theoretical view. But the approach of everybody is different.

+5

Hi! Try to point out what you have problem with, instead of posting the whole assignment. 1. You have to figure out how functions works with a variable number of arguments. You can use * in front of the argument when you implement your function for that: def f(*args): print(args) 2. You have to find a function that sum more than two arguments. You can use the bult-in sum() for that. 3. You have to put 1. and 2. together.

+2

I don't understand why is everyone giving lectures about try first do this do that. I posted coz it's been 3 days n I watched all args related videos n still I'm not able to understand. And I posted the full assignment for the ease of solving. When i posted only the question I was asked to post the whole question. The only thing you have to do is answer the question if you want to or don't if you don't want. I'm here to learn I know there's no use of getting just a certification Per Bratthammar

+2

Hi, again! In your case it is because you donโ€™t pointed out your question. The assignment are not a question, nor are the code lines. And in the heading you write about **args, that are about key word arguments, and not necessary here. I think there is for a good a reason people here not just publish the whole soloution to different assignments, but instead answare them in form of hints. A qustion about the assignment could in your case be more realated to the actual problem you have. And a good answer would give you the hints to solve the problem, by you. Thatโ€™s what I think, and why I answare your not very clear question in the way I did.

0

Jan Markus thank you sir for forgiving me๐Ÿ˜„ sir you could make an example for your learning but not make it public, while giving hints only to the questioners.

-2

sorry Jan Markus sir, but i have to downvote your answer for direct solution without hints, explanations, or feedback.๐Ÿ˜ฅ