2 votes

pourquoi le code du compteur ajoute-t-il une ligne supplémentaire à chaque comptage ?

Qu'est-ce qui ne va pas avec ce code il ajoute une ligne supplémentaire à chaque fois qu'il compte vers le haut ou vers le bas comment puis-je empêcher cela et pourquoi cela se produit-il ?

def counter(start, stop):
    x = start
    if start > stop:
        return_string = "Counting down: "
        while x >= stop:
            return_string += str(x)
            x = x-1 
            if start != stop:
                return_string += ","
            print(return_string)
    else:
        return_string = "Counting up: "
        while x <= stop:
            return_string += str(x)
            x = x + 1 
            if start != stop:
                return_string += ","
            print(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"

2voto

henrry Points 268

Utilisez ce :

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

1voto

JasonJerry Points 21
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

1voto

Shubham patel Points 21

Utilisez ça :

def counter(start, stop):
x = start
if start > 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

1voto

Ashhar Farooqui Points 11
def counter(start, stop):
x = start
if start > 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

0voto

En voici un :

def counter(start, stop):
    x = start
    if start>stop:

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

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X