1、使用while循环输入 1 2 3 4 5 6 8 9 10
count = 1 while count <= 10: if count == 7: count = count + 1 continue print(count) count = count + 1
2、求1-100的所有数的和
count = 1 sum = 0 while count <= 100: sum = sum + count count = count + 1 print(sum)
3、输出 1-100 内的所有奇数
count = 1 while count <= 100: if count % 2 ==1: print(count) count = count + 1 count = count + 1
4、输出 1-100 内的所有偶数
count = 1 while count <= 100: if count % 2 ==0: print(count) count = count + 1 count = count + 1
5、求1-2+3-4+5 ... 99的所有数的和
count = 1 sum = 0 while count <= 99: if count ==1: sum = count count = count + 1 continue else: if count % 2 ==1: sum = sum + count count = count + 1 else: sum = sum - count count = count + 1 print(sum)
6、用户登陆(三次机会重试)
count = 1 sum = 0 while count <= 3: username = input(‘请输入用户名:‘) password = input(‘请输入用户密码:‘) if username == ‘Jason‘ and password == ‘123‘: print(‘登录成功,欢迎“Jason”回来!‘) break else: sum = 3 - count print(‘用户名或密码错误,请重新输入,目前还剩余‘+str(sum)+‘机会!‘) count = count + 1