How to make a function having undefined number of arguments? | Sololearn: Learn to code for FREE!

+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.

1/24/2022 9:07:23 AM

Harsh Sinha

8 Answers

New Answer

+5

Try using non keyword arguments. I wrote an example... https://code.sololearn.com/cYbKq8l3c3XQ/?ref=app

+2

Harsh Sinha Use loop to take multiple inputs. There is no any function which will take any number of inputs.

+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)

+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-python--cms-29494

+1

Thank you very much

+1

Ok Thanks a lot

+1

By the way what are non keyword arguments?

0

Thank you