一。while 循环
1.循环:重复做某件事
2.语法
while 条件:
code1
3.结束while的方式:
1.条件不满足,下次循环开始时判断
2.break直接结束本层循环
4.while + continue
continue 之后的代码不会运行了,直接开始下次循环
n= 0
while n < 6:
if n == 4:
n+=1
continue
else:
print(n)
n+=1
5.while循环嵌套
while:
while:
while:
break
break
break
count = 1
tag = True
while tag:
name = input("name: ")
passwd = input("passwd ")
if count == 3:
print("too many")
break
if name == "chad" and passwd == "123":
print("successfull")
while tag:
print("""
1
2
3
""")
cho = input("choice:")
if cho == "1":
print(1)
elif cho == "2":
print(2)
else:
print(3)
tag = False
else:
print("error")
count +=1
6. while + else
如果while循环没有被break打断,才会执行
循环要正常结束
count = 0
while count < 3:
print(count)
count += 1
else:
print("run")
二。for 循环
循环取值简洁
for+brek
for+continue
for+else
dic = {"name":"sdfa","age":23}
for i in dic:
print(i,dic[i])
返回字符串
dic = {"name":"sdfa","age":23}
for i,r in dic.items():
print(i,r)
以字符串返回key和value
dic = {"name":"sdfa","age":23}
for i in dic.items():
print(i)
以元组返回key和value,一对key,value是一个元组
range(起始,结束,步长)
取头不取尾
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。