python学习-7 条件语句 while循环 + 练习题
1.死循环 while 1 == 1: print('ok') 结果是一直循环 2.循环 count = 0 while count < 10: print(count) count = count +1 print(error) 3.练习题 ~ 使用while循环输出1 2 3 4 5 6 8 9 10 count = 1 while count <= 10 : # 或者count < 11 if count == 7: print( ) # 也可以添加pass,什么也不执行 else: print(count) count = count + 1 执行结果: 1 2 3 4 5 6 8 9 10 Process finished with exit code 0 ~ 求1-100的所有数的和 a = 1 b = 0 while a < 101: b = b + a a = a + 1 print(b) 输出结果: 5050 Process finished with exit code 0 ~求1-100内所有的奇数 n = 1 while n < 101: js = n % 2 if js == 0: print( ) else: print(n) n = n + 1 输出结果: 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35