1 print('1、使用while循环输入 1 2 3 4 5 6 8 9 10') 2 i = 1 3 while i < 10: 4 i += 1 5 if i == 7: 6 continue #print(' ')这两个可以替换一下,看看不同的效果 7 else: 8 print(i) 9 10 11 print('2、求1-100的所有数的和') 12 i = 0 13 sum = 0 14 while i < 100: 15 i += 1 16 sum = sum + i 17 print(sum) 18 19 20 print('3、输出 1-100 内的所有奇数') 21 i = 0 22 while i < 100: 23 i += 1 24 if i %2 != 0: 25 print(i) 26 27 28 print('4、输出 1-100 内的所有偶数') 29 i = 0 30 while i < 100: 31 i += 1 32 if i %2 == 0: 33 print(i) 34 35 print('5、求1-2+3-4+5 ... 99的所有数的和') 36 i = 0 37 sum = 0 38 j = -1 39 while i < 99: 40 i += 1 41 if i %2 != 0: 42 sum = sum + (i * (-j)) 43 if i %2 == 0: 44 sum = sum + (i * j) 45 print(sum) 46 47 48 49 50
1 print('6、用户登陆(三次机会重试)') 2 i = 0 3 name = 'allison' 4 password = '123456' 5 while i < 3: 6 na = input('请输入用户名:') 7 pw = input('请输入密码:') 8 i += 1 9 oppo = 3 - i 10 if na == name and pw == password: 11 print('输入正确') 12 break 13 elif na != name or pw != password: 14 print('输入错误, 还有%d次机会'%(oppo)) 15 if i == 3: 16 answer = input('输入Y再试试?') 17 if answer == 'Y': 18 print('再来三次机会') 19 i = 0 20 else: 21 print('机会已用完')