Python loop control-While

Circulation requires a clear understanding of the core essence,
how to control the circulation body by controlling the conditions.
The flow chart looks simple. It is necessary to truly understand the logical relationship between conditions and code blocks.
Flow chart of while loop execution:
Insert picture description here
actual operation code:

# while 循环和break
n=1
while n<=100:
    if n>10: # 当n=11时,条件满足,执行break语句,跳出while循环体
        break
    print(n,end=',')
    n=n+1
print('END')

# while循环,只有条件满足,就不断循环,条件不满足时退出循环
# 在循环中,break语句可以提前退出循环
# 死循环就是循环不会终止的循环类型

i=1
sum=0
while i<=100:
    sum=sum+i
    i=i+1
print('100的累加值:',sum)

# 死循环的应用
while True:
    k=input('请输入:')
    print('您输入的是:',k)
    if k=='1':
        break
print(type(k))

The result of the above code execution:

1,2,3,4,5,6,7,8,9,10,END
100的累加值: 5050
请输入:22
您输入的是: 22
请输入:1
您输入的是: 1
<class 'str'>

Guess you like

Origin blog.csdn.net/weixin_42961082/article/details/111560792