1.简单使用for循环:for xx in xx (range(范围))
我们说可以被for循环的对象便是可迭代对象
可以for循环的数据类型:字符串,列表,元祖,字典
不可以for循环的数据类型:数字
2.while循环
a. 死循环: while true
b. 基本的: while:条件
代码块
else:
代码块
c. continue:终止当前循环,下面的代码块不会执行,直接跳回上面重新循环
break:终止整个循环
while循环练习:
1.使用while循环输入1 2 3 4 5 6 8 9
""" 使用while循环输入12345689 """ n = 1 while n < 10: if n==7: pass else: print(n) n = n+1 print("---end---")
2.输入1-100内所有的奇数
""" 输入1-100内所有的奇数 """ n = 1 while n < 101: temp = n%2 if temp == 0: pass else: print(n) n = n+1 print("---end---")
3.求1-100的所有数之和
""" 求1-100的所有数之和 """ n = 1 s = 0 while n < 101: s = s+n n = n+1 print(s)
4.求1-2+3-4+5...+99的所有数之和
""" 求1-2+3-4+5...+99的所有数之和 """ n = 1 s = 0 while n < 100: temp = n%2 if temp == 0: s = s-n else: s = s+n n = n+1 print(s)
5.用户登录(三次机会重试)
count = 0 while count < 3: user = input("请输入用户名:") pwd = input("请输入密码:") if user == "alex" and pwd =="123": print("欢迎登陆") break else: print("用户名或密码输入错误") count +=1 print("谢谢使用")