+ 1

How to make a function having undefined number of arguments?

The user should be able to enter as many inputs he wants in the function. For eg. An addition function which takes as many inputs the user enters.

24th Jan 2022, 9:07 AM
Harsh Sinha
8 Respuestas
+ 5
Try using non keyword arguments. I wrote an example... https://code.sololearn.com/cYbKq8l3c3XQ/?ref=app
24th Jan 2022, 9:32 AM
Sandy Ho
Sandy Ho - avatar
+ 2
Harsh Sinha Use loop to take multiple inputs. There is no any function which will take any number of inputs.
24th Jan 2022, 9:25 AM
A͢J
A͢J - avatar
+ 2
Harsh Sinha The above examples if you provide Hard Code values but if you want to take from user input then you have to use loop like: array = [] while True: number= int(input()) if number == 0: break array.append(number) print (array)
24th Jan 2022, 9:44 AM
A͢J
A͢J - avatar
+ 2
Non keyword arguments are usually used to prevent the program from crashing if we don't know how many arguments will be passed to the function. See the following link for more details and examples. https://code.tutsplus.com/articles/understanding-args-and-kwargs-in-JUMP_LINK__&&__python__&&__JUMP_LINK--cms-29494
24th Jan 2022, 9:59 AM
Sandy Ho
Sandy Ho - avatar
+ 1
Thank you very much
24th Jan 2022, 9:38 AM
Harsh Sinha
+ 1
Ok Thanks a lot
24th Jan 2022, 9:47 AM
Harsh Sinha
+ 1
By the way what are non keyword arguments?
24th Jan 2022, 9:53 AM
Harsh Sinha
0
Thank you
24th Jan 2022, 10:07 AM
Harsh Sinha