# 1.用for循环打印数字1到10 for x in range(1, 11): print(x) print('------------------------------------华--丽--的--分--割--线------------------------------------') # 2.用while循环打印数字1到10 # 1 2 3 4 5 6 ... 10 x = 1 while x <= 10: print(x) x += 1 print('------------------------------------华--丽--的--分--割--线------------------------------------') # 3.用for循环计算1到100的和 # 1+2+3+4+...+100 sum1 = 0 for x in range(1, 101): sum1 += x print(sum1) print('------------------------------------华--丽--的--分--割--线------------------------------------') # 4.用while循环计算1到100的和 sum1 = 0 x = 1 while x <= 100: sum1 += x x += 1 print(sum1) print('------------------------------------华--丽--的--分--割--线------------------------------------') # 5.用for循环找出列表中的偶数 nums = [19, 80, True, 45, 3.4, 78, 31, 77, 'abc'] for x in nums: if type(x) == int and x % 2 == 0: print(x) print('------------------------------------华--丽--的--分--割--线------------------------------------') # 6、用for循环计算列表中所有元素的乘积 nums = [23, 9, 56, 8, 11] product = 1 for x in nums: product *= x print(product) print('------------------------------------华--丽--的--分--割--线------------------------------------') # 7、用while循环计算阶乘(如5! = 120) # 5! = 1*2*3*4*5 num = 7 factorial = 1 for x in range(1, num+1): factorial *= x print(factorial) print('------------------------------------华--丽--的--分--割--线------------------------------------') factorial = 1 x = 1 while True: factorial *= x x += 1 if x > num: break print(factorial) print('------------------------------------华--丽--的--分--割--线------------------------------------') # 8.判断num保存的数是否是素数 # 素数: 就是质数,除了1和它本身以外没有其他的因子(不能被除了1和它本身以外的数整除的数就是素数) num = 20 # 方法1: for x in range(2, num): if num % x == 0: print(num, '不是素数!') break else: print(num, '是素数') # 方法2: for x in range(2, int(num**0.5+1)): if num % x == 0: print(num, '不是素数!') break else: print(num, '是素数') print('------------------------------------华--丽--的--分--割--线------------------------------------') # 9、用for循环计算列表中元素的平均值 nums = [23, 9, 56, 8, 11, 7, 11] sum1 = count = 0 for x in nums: sum1 += x count += 1 # round(数字, N) - 控制指定数字保留N位小数(四舍五入) print(round(sum1 / count, 2)) # 10、用while循环读取用户输入直到输入quit # while True: # value = input('请输入内容(quit表示结束):') # if value == 'quit': # break # == == == == == == == == == == == == == == = # 1.用while循环生成斐波那契数列前10项(0, 1, 1, 2, 3, 5...) n = 10 if n == 1: print(0) elif n == 2: print(0, 1) else: n1, n2 = 0, 1 print(n1, n2, end=' ') count = 0 while True: print(n1 + n2, end=' ') n1, n2 = n2, n1+n2 count += 1 if count == n - 2: break print() print('------------------------------------华--丽--的--分--割--线------------------------------------') # 2、用for循环打印九九乘法表 for row in range(1, 10): for col in range(1, row+1): print(col, '×', row, '=', col * row, sep='', end='\t') print() """ row -> 1 2 3 4 ... 9 row = 1: col -> 1 col = 1: print(1x1=1) row = 2 col -> 1, 2 col = 1: print(1x2=2) col = 2: print(2x2=4) row = 3 col -> 1 2 3 col = 1: print(1x3=3) col = 2: print(2x3=6) col = 3: print(3x3=9) row = 4 col -> 1 2 3 4 col = 1: print(1x4=4) col = 2: print(2x4=8) col = 3: print(3x4=12) col = 4: print(4x4=16) row = 5 .... 1x1=1 1x2=2 2x2=4 1x3=3 2x3=6 3x3=9 1x4=4 2x4=8 3x4=12 4x4=16 ... """ print('------------------------------------华--丽--的--分--割--线------------------------------------') # 3、用while循环实现猜数字游戏(随机数 + 用户输入) """ 30 请输入一个整数(0~100):50 大了,再小点儿! 请输入一个整数(0~100):25 小了,再大点儿! 请输入一个整数(0~100):35 大了,再小点儿! ... 请输入一个整数(0~100):30 恭喜你,猜对了! """ # 产生随机数的代码: # from random import randint # num = randint(0, 100) # 产生0~100的随机数 # while True: # value = int(input('请输入一个整数(0~100):')) # if value > num: # print('大了,再小点儿!') # elif value < num: # print('小了,再大点儿!') # else: # print('恭喜你,猜对了!!') # break print('------------------------------------华--丽--的--分--割--线------------------------------------') # 4.用while循环模拟用户登录(最多3次尝试, 正确用户名和密码可以自己设置一个固定的) # user = 'admin' # pw = '123123' # count = 0 # while True: # print('==========xxx系统登录页面============') # u = input('请输入账号:') # p = input('请输入密码:') # count += 1 # # if u == user and p == pw: # print('登录成功!') # break # else: # print('账号或者密码错误,请重试!') # # if count == 3: # print('已经超过了3次!账号被冻结!') # break print('------------------------------------华--丽--的--分--割--线------------------------------------') # 5. 用while循环计算数字的位数 # 123 -> 3 10098 -> 5 89 -> 2 8 -> 1 # 123 // 10 -> 12 // 10 -> 1 // 10 -> 0 # 10098 // 10 -> 1009 // 10 -> 100 // 10 -> 10 // 10 -> 1 // 10 -> 0 # 89 // 10 -> 8 // 10 -> 0 # 8 // 10 -> 0 num = -283 if num < 0: num = -num count = 0 while True: num //= 10 count += 1 if num == 0: break print(count) print('------------------------------------华--丽--的--分--割--线------------------------------------') # 6.用while循环反转一个数字(如输入123,输出321) num = 1098 result = 0 while True: # print(num % 10, end='') result = result * 10 + num % 10 num //= 10 if num == 0: break print(result) # 0*10+8 -> 8 # 8*10 + 9 -> 89 # 89 * 10 + 0 -> 890 # 890*10 + 1
17.homework
本节562字2025-03-29 12:53:48