python基础之课业习题(一)
1、使用while循环输入 1 2 3 4 5 6 8 9 10
1 a = 0 2 while a<10: 3 a += 1 4 if a == 7: 5 print(' ') 6 continue 7 print(a)
2、求1-100的所有数的和
1 b = 1 2 c = 0 3 while b<101: 4 c = c + b 5 b += 1 6 print(c)
3、输出 1-100 内的所有奇数
例(一)
1 a = 1 2 while a<100: 3 print(a) 4 a = a + 2
例(二)
1 d = 1 2 while d < 101: 3 e = d%2 4 if e == 1: 5 print(d) 6 d += 1
4、输出 1-100 内的所有偶数
例(一)
1 f = 1 2 while f < 101: 3 g = f%2 4 if g == 0: 5 print(f) 6 f += 1
例(二)
1 a=0 2 while a<99: 3 a=a+2 4 print(a)
5、求1-2+3-4+5 ... 99的所有数的和
例(一)
1 n = 1 2 m = 0 3 while n <=100: 4 if n % 2 == 0: 5 m += n 6 else: 7 m -= n 8 n += 1 9 print(m)
6、用户登陆(三次机会重试)
1 a = 1 2 while a < 4 : 3 username = input('请键入用户名:') 4 password = input('请键入密码') 5 a = a + 1 6 if username == 'happying' and password == '000000': 7 print('Welcom!!!') 8 break 9 if a == 4: 10 print('Sorry!!!') 11 continue 12 else: 13 print('Please try again')

浙公网安备 33010602011771号