0
def count_down_skip(start, skip=0):
    """
    Counting down a sequence with a skip value,
    from a defined start point in reversed order.

    Args:
        start: start loop index.
        skip: number to skip over.

    Returns:
        (list): skipped list.

    """
    return [num for num in reversed(range(start + 1)) if num != skip]


print("... ".join(map(str, count_down_skip(10,1))) + "!")

this code can output 10 to 0 and without 1 while if 10 to 0 without 1 4 3 (skip these numbers), then how can I do? I tried change the print :

print("... ".join(map(str, count_down_skip(10,1,4,3))) + "!")

but error happened...

define a function for countdown numbers in python

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
ssssmaner
  • 65
  • 5

5 Answers5

3
def count_down_skip(start, *args):
    return [num for num in reversed(range(start + 1)) if num not in args]


print("... ".join(map(str, count_down_skip(10,1,4,3))) + "!")

You cannot input more arguments than in the function definition. You can however use *args as a parameter. This allows to input variable number of parameters to the function.

2

You can take the list of numbers to be skipped. You then compare each list item with the one in range(num+1).

def count_down_skip(start, skip = []):
    return [num for num in reversed(range(start + 1)) if num not in skip]

print(count_down_skip(10,[1,4,3]))
#[10, 9, 8, 7, 6, 5, 2, 0]
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Sid
  • 2,174
  • 1
  • 13
  • 29
  • It'd be better to use an empty **tuple** as the default arg, `skip=()`, since [default args are mutable](https://docs.python-guide.org/writing/gotchas/#mutable-default-arguments), though I can't think of any ways that could be a problem off the top of my head. – wjandrea Apr 27 '21 at 17:09
1

You can use *args, and check if num is a member:

def count_down_skip(start, *skips):
    return [num for num in reversed(range(start+1)) if num not in skips]

print(count_down_skip(10, 1, 4, 3))

Output:

[10, 9, 8, 7, 6, 5, 2, 0]
wjandrea
  • 28,235
  • 9
  • 60
  • 81
1

Best Solution is,

def count_down_skip(start, *skip):
    return [num for num in reversed(range(start + 1)) if num not in skip]


print("... ".join(map(str, count_down_skip(10,1,4,3))) + "!")
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Khasim
  • 89
  • 4
  • 1
    Why is this the best solution? Like, what advantages does it have over [using a list](https://stackoverflow.com/a/67287345/4518341) for example? You can [edit] to clarify, which will help OP understand. – wjandrea Apr 27 '21 at 17:11
0

You have too many arguments in the count_down_skip function. You could use a list if you want to input multiple numbers, like this:

def count_down_skip(start, skip=0):
    """
    Counting down a sequence with a skip value,
    from a defined start point in reversed order.

    Args:
        start: start loop index.
        skip: number to skip over.

    Returns:
        (list): skipped list.

    """
    return [num for num in reversed(range(start + 1)) if num not in skip]


print("... ".join(map(str, count_down_skip(10, [1,4,3]))) + "!")
  • `skip=0` is a problem. If you run `count_down_skip(10)`, you'll get `TypeError: argument of type 'int' is not iterable`. Better to use an empty tuple instead: `skip=()`. BTW, the docstring is inconsistent with the code. – wjandrea Apr 27 '21 at 17:13