2

I want to make a function same range() for example func_range() that has three arguments for it func_range(start , end , step ) but when recall the function without parameter I want to print for example "please set the argument " .

how could I check the existence of argument in function

func_range(5) :     print 1 to 5 
func_range(1 , 5) : print 1 to 5
func_range() : print "please set the argument"
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • 1
    You can also use default argument values instead of variable function arguments here. Note that instead of printing that error message, incorrect usages should throw an exception, so it’s best not to change the default behavior here. – poke Jan 04 '17 at 09:31

2 Answers2

1

Either give default values to parameters or you can args keyword

def func_range(*args):
    if not args:
         print('Please set the argument')

    start, end = args
hspandher
  • 15,934
  • 2
  • 32
  • 45
  • by this method ( one arguments ) How can I use another start , end and stop parameter in this function – majid Hakimi Jan 04 '17 at 09:54
  • You can use args as I mentioned I have updated in my answer or you can use kwargs – hspandher Jan 04 '17 at 10:39
  • I like use your solution about arg but the end line don't work ! (start, end = args ) if possible write the complete function for me . How find out in function the user enter one or two or three arguments and if user enter more than 3 argument print error message . if user enter one argument consider it as end and count 1 to end by 1 step and if enter 2 argument consider start and end by step=1 and if enter start , end and step count to start to stop by step parameter – majid Hakimi Jan 05 '17 at 17:14
0

You can use optional arguments like this:

>>> def f(start=None, end=None, step=None):
...     if (start == None and end == None and step == None):
...             print("please set the argument")
...     else:
...             pass #do whatever you wish
... 
>>> f()
please set the argument
Graham
  • 7,431
  • 18
  • 59
  • 84
Ohad Eytan
  • 8,114
  • 1
  • 22
  • 31
  • How find out in function the user enter one or two or three arguments and if user enter more than 3 argument print error message . if user enter one argument consider it as end and count start to end by 1 step and if enter 2 argument consider start and end by step 1 and if enter start , end and step count to start to stop by step parameter – majid Hakimi Jan 05 '17 at 17:01