Reputation: 113
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
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
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
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
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
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
Reputation: 73470
Some of the errors you made:
break
the loop on the first iterationif x == stop
can never be true because of the enclosing loop's termination condition while x > stop
stop
itself is never added to the outputThe 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