while循坏

while 条件:

  条件成立,执行循坏体(注意,while循坏必须有结束条件,不然会进入死循坏)

简单做个演示:

# -*- coding:utf-8 -*-
# Author:覃振鸿
#猜年龄 while + if
guess_age = 56 #正确的年龄
count = 0 #计数器
while count<3: #循环三次,通过判断计数器
age=int(input("you think how age:"))
if(age == guess_age):
print("Yes,you got it")
break #破坏循环
elif(age>guess_age):
print("tink small...")
else:
print("tink bigger...")
else:
print("you have tried too many times,fuck off")

单层for循坏

for 条件:(条件可以是次数,也可以对数组进行循坏遍历)

  条件成立执行循环体

多层for循坏

for 条件:

  条件成立执行循环体

  for 条件:

    条件成立执行循环体

简单demo:

# -*- coding:utf-8 -*-
# Author:覃振鸿
#for循环
#最简单的for循坏
print("------最简单的for循坏---------")
for i in range(10):
print("loop",i)
#打印偶数
print("------打印偶数---------")
for i in range(0,10,2): #2是打印间隔
print("loop",i)
#循环嵌套
print("------循环嵌套---------")
for i in range(10):
print("------------外层",i)
for j in range(10):
print(j)

大家多多指教,技术需要交流

05-11 23:01