+ 1
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)
9 Respuestas
+ 6
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.
+ 3
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.
+ 1
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.
+ 1
Ipang Hi!
But the comment was made two years ago…
+ 1
nelisa6304
So how do you ser this is connected to functional argumens?
0
Hello
def adder(*args):
print(sum(args))
adder(2, 3)
adder(2, 3, 4)
adder(1, 2, 3, 4, 5)
This function uses *args to accept any number of arguments and then sums them using the sum() function.
Regards
David Warner
Purva Aerocity
https://www.purvaaerocity.gen.in/
0
Let's go over what happens when you call adder with different numbers of arguments:
adder(2, 3): args will be (2, 3), so sum(args) is 2 + 3 = 5.
adder(2, 3, 4): args will be (2, 3, 4), so sum(args) is 2 + 3 + 4 = 9.
adder(1, 2, 3, 4, 5): args will be (1, 2, 3, 4, 5), so sum(args) is 1 + 2 + 3 + 4 + 5 = 15.
https://sarahjosbeauty.com/products/sarah-jo-s-beauty-chateau-non-cbd-toner
- 2
sorry Jan Markus sir, but i have to downvote your answer for direct solution without hints, explanations, or feedback.😥