计数器增加或减少脚本不起作用 python

Counter up or down script not working python

我正在尝试创建一个脚本,如果 start < stop 或倒计时是 start < stop 则计数。我得到了部分输出但不是全部。有什么建议吗?

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"

您犯的一些错误:

  1. 你已经break第一次迭代的循环
  2. 只有在到达停止时才添加逗号,这正是不再需要逗号的时候
  3. 由于封闭循环的终止条件 while x > stop
  4. if x == stop 永远不可能为真
  5. 出于同样的原因,stop 本身永远不会添加到输出中

以下更改将修复您的功能:

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'
    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
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"

为什么不使用 built-in range 函数获取数字列表,然后使用 join 在值之间放置逗号。更简洁。

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

给予

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

这对我有用

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