Sergei
Sergei

Reputation: 605

return to the top of a loop

At print("you dont have that many cards!") i want the while loop to start over from

print("how many cards in heap no:", n, end="")

instead of breaking. How can this be done?

y = []
def cardHeaps():
    global cards
    n = 1
    while int(cards) > 0:
        print("how many cards in heap no:", n, end="")
        x = int(input("? "))
        cards = int(cards)
        if x > cards:
            print("you dont have that many cards!")
            break
        y.append(x)
        cards -= int(x)
        print(cards, " cards left")
        n += 1
        if cards <= 0:
            print("out of cards!")
            break

Upvotes: 6

Views: 46861

Answers (2)

love_me_some_linux
love_me_some_linux

Reputation: 2741

Use continue instead of break.

Upvotes: 12

Jacob
Jacob

Reputation: 43219

You have to use continue instead of break.

Python docs on continue

Upvotes: 18

Related Questions