编程中的循环与图像处理技巧
1. 循环基础与应用
在编程里,循环是极为关键的结构,它能够让特定的代码块反复执行。Python 提供了 while 和 for 两种主要的循环类型。
- while 循环 :只要条件为真, while 循环就会持续执行代码块。例如,在获取目标环数时,代码会检查输入的环数是否在合法范围内,若不在,就会提示用户重新输入:
# Obtain number of rings in the target.
numRings = int(input("Enter # of rings in the target: "))
MIN_NUM_RINGS = 1
MAX_NUM_RINGS = 10
while numRings < MIN_NUM_RINGS or numRings > MAX_NUM_RINGS :
print("Error: the number of rings must be between",
MIN_NUM_RINGS, "and", MAX_NUM_RINGS)
numRings = int(input("Re-enter # of rings in the target: "))
- for 循环 :
for循环常用于遍历容器中的元素,像字符串、列表等。例如,要计算 1 到n
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



