Python while循环

while 适合条件成立就一直重复的场景。

k = 1
while k < 5:
    print(k)
    k += 1

关键是循环条件必须有机会变成 False,否则会死循环。

相关:Python循环语句Python条件判断