返回循环的顶部

Ser*_*gei 2 python

print("you dont have that many cards!")我希望while循环重新开始

print("how many cards in heap no:", n, end="")
Run Code Online (Sandbox Code Playgroud)

而不是打破.如何才能做到这一点?

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
Run Code Online (Sandbox Code Playgroud)

Jac*_*cob 13

你必须使用continue而不是break.

Python文档继续


lov*_*nux 10

使用continue而不是break.