1、使用while循环输入 1 2 3 4 5 6 8 9 10
i = 1 while i <=10 : if i != 7: print(i) else: print(' ') i += 1
2、求1-100的所有数的和
i = 1 sum = 0 while i <= 100: sum += i i += 1 print(sum)
3、输出 1-100 内的所有奇数
i = 1 while i <= 100: print(i) i += 2
4、输出 1-100 内的所有偶数
i = 2 while i <= 100: print(i) i += 2
5、求1-2+3-4+5 ... 99的所有数的和
i = 1 sum = 0 while i < 100: if i%2 == 1: sum += i else: sum -= i i += 1 print(sum)
6、用户登陆(三次机会重试)
i = 1 while i <= 3: name = input('请输入用户名:') password = input('请输入密码:') if name == '悟空' and password == '123': print('登陆成功') break else: print('登陆失败') i += 1
7、求1-2+3....+99除了88以外所有数的和
i = 1 sum = 0 while i <= 99: if i == 88: i += 1 continue if i % 2 == 1: sum += i else: sum -= i i += 1 print(sum)
8、用户登录(三次机会)并且每次输错时显示剩余登录机会
i = 1 while i <= 3: name = input('请输入用户名:') password = input('请输入密码:') if name == '悟空' and password == '123': print('登陆成功') break else: print('登陆失败,您还剩%d次登陆机会'%(3-i)) i += 1
9、用户登录(三次机会)并且每次输错时显示剩余登录机会,当剩余机会为0时,可以询问用户是否在试试,如果用户同意在尝试,那就将在给他三次机会(一直循环)
i = 1 while i <= 3: name = input('请输入用户名:') password = input('请输入密码:') if name == '悟空' and password == 123: print('登陆成功') break else: print('登陆失败,您还有%d次登陆机会'%(3-i)) if i == 3: choice = input('是否需要重新获得三次登陆机会?Y/N:') if choice == 'Y': i = 0 i += 1
10、用户登录(三次机会)并且每次输错时显示剩余登录机会,当剩余机会为0时,可以询问用户是否在试试,如果用户同意在尝试,那就将在给他三次机会(一共6次)
i = 2 j= 1 while i >= 0: name = input('请输入用户名:') password = input('请输入密码:') if name == '悟空' and password == '123': print('登陆成功') break else: if i == 0 and j == 1: k = input('登陆失败,是否需要重新获得三次登陆机会?Y/N:') if k == 'Y' and j == 1: i = 3 j -= 1 elif i == 0 and j == 0: print('登陆失败,您的账号已经被锁上') else: print('登陆失败,您还有%d次机会' % i) i -= 1