inkblot
inkblot

Reputation: 113

Counter up or down script not working python

I'm trying to create a script that counts up if start < stop or count down is start < stop. I'm getting part of the output but not all. Any tips?

def counter(start, stop):
x = start
if x > stop:
    return_string = "Counting down: "
    while x > stop:
        return_string += str(x)
        if x == stop:
            return_string += ","
        return return_string
else:
    return_string = "Counting up: "
    while x <= stop:
        return_string += str(x)
        if x == stop:
            return_string += ","
        break
return return_string
         print(counter(1, 10)) # Should be "Counting up: 1,2,3,4,5,6,7,8,9,10"
         print(counter(2, 1)) # Should be "Counting down: 2,1"
         print(counter(5, 5)) # Should be "Counting up: 5"

Upvotes: 1

Views: 18089

Answers (6)

Mohammad Sohel
Mohammad Sohel

Reputation: 16

every answers is almost correct but no one noticed that their code added an extra "," at the last of every answers

def counter(start, stop):
    x = start
    if x>stop:
        return_string = "Counting down: "
        while x >= stop:
            return_string += str(x)
            x = x-1
            if x >= stop:
                return_string += ","
    else:
        return_string = "Counting up: "
        while x <= stop:
            return_string += str(x)
            x = x + 1
            if x <= stop:
                return_string += ","
    return return_string

print(counter(1, 10)) # Should be "Counting up: 1,2,3,4,5,6,7,8,9,10"
print(counter(2, 1)) # Should be "Counting down: 2,1"
print(counter(5, 5)) # Should be "Counting up: 5"

Upvotes: 0

Jean Mitchelle
Jean Mitchelle

Reputation: 11

THIS WORKED FOR ME

def counter(start, stop):
x = start
if x>stop:
    return_string = "Counting down: "
    while x >= stop:
        return_string += str(x)
        if x!=stop:
            return_string += ","
        x-=1
else:
    return_string = "Counting up: "
    while x <= stop:
        return_string += str(x)
        if x!=stop:
            return_string += ","
        x+=1
return return_string

Upvotes: 1

scotty3785
scotty3785

Reputation: 7006

Why not use the built-in range function to get the list of numbers and then use join to place the comma between the values. Much more succinct.

def counter(start,stop):
    if start <= stop:
        output = "Counting Up: "
    else:
        output = "Counting Down: "
    direction = 1 if start <= stop else -1
    output += ",".join(map(str,range(start,stop+direction,direction)))
    return output

print(counter(1, 10)) # Should be "Counting up: 1,2,3,4,5,6,7,8,9,10"
print(counter(2, 1)) # Should be "Counting down: 2,1"
print(counter(5, 5)) # Should be "Counting up: 5

Gives

Counting Up: 1,2,3,4,5,6,7,8,9,10
Counting Down: 2,1
Counting Down: 5

Upvotes: 0

Manushi Patel
Manushi Patel

Reputation: 9

def counter(start, stop):
    x = start
    if x > stop:
        return_string = "Counting down: "
        while x >= stop:
            return_string += str(x)
            if x > stop:
                return_string += ","
            x -= 1 
    else:
        return_string = "Counting up: "
        while x <= stop:
            return_string += str(x)
            if x < stop:
                return_string += ","
            x +=1
    return return_string

print(counter(1, 10)) # Should be "Counting up: 1,2,3,4,5,6,7,8,9,10"
print(counter(2, 1)) # Should be "Counting down: 2,1"
print(counter(5, 5)) # Should be "Counting up: 5"

Upvotes: 0

Parthasd
Parthasd

Reputation: 1

    def counter(start, stop):
        x = start
        if x > stop:
            return_string = "Counting down: "
            while x > stop:
                return_string += str(x)+","
                x -= 1
        else:
            return_string = "Counting up: "
            while x < stop:
                return_string += str(x)+","
                x += 1
        return_string += str(stop)+'"'
        return return_string
print(counter(1, 10)) # Should be "Counting up: 1,2,3,4,5,6,7,8,9,10"
print(counter(2, 1)) # Should be "Counting down: 2,1"
print(counter(5, 5)) # Should be "Counting up: 5

Upvotes: -2

user2390182
user2390182

Reputation: 73470

Some of the errors you made:

  1. You already break the loop on the first iteration
  2. You add the comma only when you reach stop which is exactly when no comma is needed anymore
  3. that if x == stop can never be true because of the enclosing loop's termination condition while x > stop
  4. For the same reason, stop itself is never added to the output

The following changes will fix your function:

def counter(start, stop):
    x = start
    if x > stop:
        return_string = "Counting down: "
        while x > stop:
            return_string += str(x)+","
            x -= 1
    else:
        return_string = "Counting up: "
        while x < stop:
            return_string += str(x)+","
            x += 1
    return_string += str(stop)
    return return_string

>>> counter(1,2)
'Counting up: 1,2'
>>> counter(1,5)
'Counting up: 1,2,3,4,5'
>>> counter(5,1)
'Counting down: 5,4,3,2,1'
>>> counter(5,2)
'Counting down: 5,4,3,2'

Upvotes: 4

Related Questions