遵循规则以使用while循环
- 尽量少用 while循环,大部分时候for循环是更好的选择。
- 重复检查你的 while语句,确定你测试的布尔表达式最终会变成 False。
- 如果不确定,就在 while循环的开始和结尾打印岀你要测试的值,看看它的变化。
结束循环
break
退出循环
continue
结束本次循环,继续下一次循环
i = 0
numbers = []
# 开始一个while循环
while i < 6:
print(f"At the top i is {i}")
# 将i的值追加写在列表的最后
numbers.append(i)
i = i + 1
print("Numbers now: ", numbers)