+ 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)
6 Answers
+ 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.
+ 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.
0
Things to Remember:
https://www.dunkinrunsonyou.run/
*args and **kwargs are special keyword which allows function to take variable length argument.
*args passes variable number of non-keyworded arguments and on which operation of the tuple can be performed.
- 2
sorry Jan Markus sir, but i have to downvote your answer for direct solution without hints, explanations, or feedback.😥
Hot today
Python — File Handling
0 Votes
Help me solve this (using loop)
2 Votes
What is wrong? Error on test.
1 Votes
Help me wiht python
1 Votes
Question is write a c program to print prime numbers up to n and print the largest number in array.
1 Votes
Help me
0 Votes