开发者

return to the top of a loop

开发者 https://www.devze.com 2023-03-27 03:32 出处:网络
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=\"\")

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 += 开发者_如何学Python1
        if cards <= 0:
            print("out of cards!")
            break


You have to use continue instead of break.

Python docs on continue


Use continue instead of break.

0

精彩评论

暂无评论...
验证码 换一张
取 消