返回循环顶部

5 投票
2 回答
46602 浏览
提问于 2025-04-16 23:23

print("你没有那么多卡片!") 这行代码时,我希望这个循环能从

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

重新开始,而不是直接结束。这样该怎么做呢?

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

2 个回答

12

continue 代替 break

18

你需要用 continue 来代替 break

关于 continue 的 Python 文档

撰写回答