1.使用while循环输入 1 2 3 4 5 6 8 9 10
1 # 有空格 2 i = 0 3 while i < 10: 4 i += 1 5 if i == 7: 6 print(' ') 7 else: 8 print(i) 9 10 # 没空格 11 i = 0 12 while i < 10: 13 i += 1 14 if i == 7: 15 continue 16 print(i) 17 18 # 没空格 19 i = 0 20 while i < 10: 21 i += 1 22 if i == 7: 23 pass 24 else: 25 print(i)
2.求1-100的所有数的和
1 i = 0 2 sum = 0 3 while i < 100: 4 i += 1 5 sum += i 6 print(sum)
3.输出 1-100 内的所有奇数
1 # 法一 2 i = 0 3 while i < 100: 4 i += 1 5 if i%2 == 1: 6 print(i) 7 8 # 法二 9 i = 0 10 while i < 100: 11 i += 1 12 if i%2 == 0: 13 continue 14 print(i) 15 16 # 法三 17 i = 1 18 while i < 100: 19 print(i) 20 i += 2
4.输出 1-100 内的所有偶数
1 # 法一 2 i = 0 3 while i < 100: 4 i += 1 5 if i%2 == 0: 6 print(i) 7 8 # 法二 9 i = 0 10 while i < 100: 11 i += 1 12 if i%2 == 1: 13 continue 14 print(i) 15 16 # 法三 17 i = 2 18 while i < 101: 19 print(i) 20 i += 2
5.求1-2+3-4+5 ... 99的所有数的和
1 i = 1 2 sum = 0 3 while i < 100: 4 if i%2 == 1: 5 sum += i 6 else: 7 sum -= i 8 i += 1 9 print(sum)
6.用户登录,三次机会重试
1 #自己写的... 2 count = 3 3 username = 'yyh' 4 password = '123' 5 while count > 0: 6 name = input('请输入用户名:') 7 if name == username: 8 while count > 0: 9 word = input('请输入密码:') 10 if word == password: 11 count = -1 12 print('登录成功!') 13 else: 14 count -= 1 15 tip = '密码错误,你还有%d次机会'%(count) 16 print(tip) 17 else: 18 count -= 1 19 tip = '用户名错误,你还有%d次机会' %(count) 20 print(tip) 21 if count == 0: 22 print('登录失败,三次机会已用完')
1 i = 0 2 while i <3: 3 username = input('请输入账号:') 4 password = input('请输入密码:') 5 if username == 'yyh' and password == '123': 6 print('登陆成功') 7 else: 8 print('登录失败,请重新登录') 9 i += 1 10 print('三次机会已用完')