1、使用while循环输出 1、2、3、4、5、6、8、9、10
# -*- conding:utf-8 -*- n=1 while n<=10: if n!=7: print(n) n+=1
2、求1-100数的和
1 # -*- conding:utf-8 -*- 2 n=1 3 sum1=0 4 while n<=100: 5 sum1+=n 6 n+=1 7 print(sum1)
3、输出1-100所有的奇数
1 # -*- conding:utf-8 -*- 2 n=1 3 while n<=100: 4 if n%2==1: 5 print(n) 6 n+=1
4、输出1-100所有的偶数
1 # -*- conding:utf-8 -*- 2 n=1 3 while n<=100: 4 if n%2==0: 5 print(n) 6 n+=1
5、求1-2+3-4+5·······99所有数的和
1 # -*- coding:utf-8 -*- 2 n=1 3 sum1=0 4 while n<=100: 5 if n%2==1: 6 sum1+=n 7 elif n%2==0: 8 sum1-=n 9 n+=1 10 print(sum1)
6、用户有三次登陆的机会
1 # -*- coding:utf-8 -*- 2 n=3 3 while n>0: 4 user_name=input("请输入登陆用户名:") 5 password=input("请输入密码:") 6 n-=1 7 if user_name=="zf": 8 if password=="123456": 9 print("登陆成功") 10 break 11 else: 12 print("输入的密码错误") 13 continue 14 else: 15 print("输入的用户名错误") 16 continue 17 if n==0: 18 print("登陆失败,密码已锁住")