# 小球落地,一共运动了多少米 hight = 100 # 原始高度 distance = 0 # 和 for i in range(10): # 将 下落 高度加入到 和 中 distance += hight # distance = distance + hight if i == 9: break # 计算 反弹 告诉 hight /= 2 # hight = hight / 2 # 将 反弹高度 加入到和中 distance += hight # distance = distance + hight # if i == 9: # distance -= hight print(f"共经历了{distance}米")
# 九九乘法表 num = 0 for i in range(1,10): # for j in range(1,10): for j in range(1,i+1): num += 1 if j > i: # continue # 81 break # 53 print(f"{j}*{i}={j * i}",end="\t") # print("\n",end="") print() print(num)
import random # 300名员工,年会抽奖,奖项如下 # 一等奖3名 # 二等奖6名 # 3等奖30名 # 要求:共轴3次,第一次抽3等奖,第二次抽2等奖,第三次抽1等奖,抽中的人不能再抽取 staff_list = [] for i in range(1,301): staff_list.append(f"员工{i}") level = [30, 6, 3] count = 0 # 1. for j in range(3): 里面j 不是一定要用到它的 # 2-2.1 j count 的取值范围相同 # 2-2.2 j count 的数值运动轨迹相同 for j in range(3): winnerList = random.sample(staff_list, level[j]) for winner in winnerList: staff_list.remove(winner) print(f"恭喜获得{3-j}等奖的是:{winnerList}") print(f"还剩{len(staff_list)}个人未中奖")
验证码功能:
import random import string def get_code(count): code = "" for i in range(count): c1 = string.ascii_lowercase print(type(c1)) c2 = string.ascii_uppercase c3 = string.digits # all_code = c1 + c2 + c3 code += random.choice(c1+c2+c3) return code print(get_code(4))