'Need some help on the following python script
this is the following question and the script - The counter function counts down from start to stop when start is bigger than stop, and counts up from start to stop otherwise. Fill in the blanks to make this work correctly.
Python Script -
def counter(start, stop):
x = start
if ___:
return_string = "Counting down: "
while x >= stop:
return_string += str(x)
if ___:
return_string += ","
___
else:
return_string = "Counting up: "
while x <= stop:
return_string += str(x)
if ___:
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"
Solution 1:[1]
Looks like homework but anyways,
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 = x - 1
else:
return_string = "Counting up: "
while x <= stop:
return_string += str(x)
if x<stop:
return_string += ","
x = x + 1
return return_string
Solution 2:[2]
def counter(start, stop):
x = start
if start > stop:
return_string = "Counting down: "
while x >= stop:
return_string += str(x)
x -= 1
if start != stop:
return_string += ","
else:
return_string = "Counting up: "
while x <= stop:
return_string += str(x)
x += 1
if start != stop:
return_string += ","
return return_string.strip(",")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ShlokSayani |
| Solution 2 | Arham Ahmed |
