-1

Currently I have a working range function that accepts and outputs the first 3 arguments, but I'm lost on how to add the optional 4th argument of a separator/end. I'm not sure if I should be using sep or end.

def Practice(start, stop, step):
for i in range(start, stop+1, step):

    if i%step == 0:

        print(i, end=' ')

So if a user types in the shell: Practice(4,24,4) it will print: 4 8 12 16 20 24

jackrabbit
  • 87
  • 9

3 Answers3

0

Try this: In below code default value of step will be 4 if you dont pass any value for it

e.g.1  rangePractice(4,24) # will have step=4 and separator = "  "
e.g.2  rangePractice(4,24,5) # will step=4 and separator = "  "
e.g.3  rangePractice(4,24,4, "-") # will have step=4 and separator = "-"
def rangePractice(start, stop, step=4, separator = "  "):
    for i in range(start, stop+1, step):
        if i%step == 0:
            print(i, end=separator)

Umesh
  • 941
  • 5
  • 12
  • can the same be done for step? So say the user doesn't input a number for step; if the user only adds two numbers: rangePractice(4,24) , would that make it so the default value is increments of 1? – jackrabbit Apr 15 '19 at 17:27
  • yes can be done with step also. You can make any argument optional, just pass rangePractice(start, stop, step=4, separator = " ") – Umesh Apr 15 '19 at 17:28
  • @lsimon: Yes, you can have as much arguments with default values as you want. But keep in mind that the ones with default values always have to be after the mandatory ones. – Opifex Apr 15 '19 at 17:29
0

sep is used to separate the arguments, and end after the last argument. They are used by print function.

>>> print('boa', 'cat', 'dog', sep='=>', end='!!!\n')
boa=>cat=>dog!!!

So, you need end here in your program. If you use sep, end will be treated as \n and all line will be printed in new line.

def rangePractice(start, stop, step, separator = "  "):
    for i in range(start, stop+1, step):
        if i%step == 0:
            print(i, end=separator)

I suggest to practice such things, without getting spoon feeded from stackoverflow, it will boost up your knowledge.

Kamal Nayan
  • 1,890
  • 21
  • 34
-1

Let the parameter have a default value. Optionally, in the function-body you can do a check if the user passed his own parameter (But it's not necesarry for your use case).

def rangePractice(start, stop, step, separator=' '):
  for i in range(start, stop+1, step):

    if i%step == 0:

        print(i, end=separator)
Opifex
  • 362
  • 4
  • 15
  • Avoid using any reserved keywords for variable names: `sep` – Kamal Nayan Apr 15 '19 at 17:24
  • @KamalNayan `sep` is not a reserved keyword or builtin, what's wrong with it as a variable name? – Chris_Rands Apr 15 '19 at 17:32
  • @Chris_Rands: Nothing wrong, just a convention. `sep` is used by print function as argument. Beauty of python, that it never complains on such things – Kamal Nayan Apr 15 '19 at 17:35
  • @KamalNayan There is no convention to avoid using keyword arguments as variable names, that would be silly (how would you even get a list of such names?). Here, they are trying to emulate `range()` so it makes sense to use the same name IMO. True keywords cannot be used as variable names anyway, e.g. `for = 1` is invalid syntax – Chris_Rands Apr 15 '19 at 17:38