分支与循环综合实战
通过实战练习巩固分支和循环的使用技巧。目录基础练习算法练习游戏开发数据处理基础练习练习 1判断闰年yearint(input(请输入年份))if(year%40andyear%100!0)or(year%4000):print(f{year}是闰年)else:print(f{year}不是闰年)练习 2成绩等级scorefloat(input(请输入分数))ifscore90:gradeAelifscore80:gradeBelifscore70:gradeCelifscore60:gradeDelse:gradeEprint(f成绩等级{grade})练习 3计算器num1float(input(第一个数字))operatorinput(运算符、-、*、/)num2float(input(第二个数字))ifoperator:resultnum1num2elifoperator-:resultnum1-num2elifoperator*:resultnum1*num2elifoperator/:resultnum1/num2else:print(无效的运算符)exit()print(f{num1}{operator}{num2}{result})算法练习练习 4斐波那契数列# 打印前 N 个斐波那契数nint(input(要打印多少个斐波那契数))a,b0,1for_inrange(n):print(a,end )a,bb,ab练习 5素数判断numint(input(请输入一个数字))ifnum2:print(f{num}不是素数)else:foriinrange(2,int(num**0.5)1):ifnum%i0:print(f{num}不是素数)breakelse:print(f{num}是素数)练习 6水仙花数# 找出所有 3 位数的水仙花数fornuminrange(100,1000):# 分解各位数字hundredsnum//100tens(num%100)//10unitsnum%10# 判断是否为水仙花数ifnumhundreds**3tens**3units**3:print(num)游戏开发练习 7猜数字游戏importrandom# 生成随机数secretrandom.randint(1,100)attempts0print(猜数字游戏我已经想好了一个 1-100 之间的数字。)whileTrue:guessint(input(你的猜测))attempts1ifguesssecret:print(f恭喜你猜对了用了{attempts}次。)breakelifguesssecret:print(太小了)else:print(太大了)练习 8石头剪刀布importrandom choices[石头,剪刀,布]user_score0computer_score0whileuser_score3andcomputer_score3:userinput(石头、剪刀、布输入 quit 退出)ifuserquit:breakifusernotinchoices:print(无效输入)continuecomputerrandom.choice(choices)print(f你出{user}电脑出{computer})ifusercomputer:print(平局)elif(user石头andcomputer剪刀)or\(user剪刀andcomputer布)or\(user布andcomputer石头):print(你赢了)user_score1else:print(电脑赢了)computer_score1print(f比分你{user_score}-{computer_score}电脑)print(f最终比分你{user_score}-{computer_score}电脑)数据处理练习 9找最大值和最小值numbers[23,45,12,67,34,89,56,78,90,43]max_numnumbers[0]min_numnumbers[0]fornuminnumbers:ifnummax_num:max_numnumifnummin_num:min_numnumprint(f最大值{max_num})print(f最小值{min_num})练习 10统计成绩scores[85,92,78,95,88,76,90,82,94,79]total0forscoreinscores:totalscore averagetotal/len(scores)print(f总分{total})print(f平均分{average:.2f})print(f最高分{max(scores)})print(f最低分{min(scores)})# 统计各等级grades{A:0,B:0,C:0,D:0,E:0}forscoreinscores:ifscore90:grades[A]1elifscore80:grades[B]1elifscore70:grades[C]1elifscore60:grades[D]1else:grades[E]1print(成绩分布,grades)练习 11密码检查器passwordinput(请输入密码)# 检查密码强度iflen(password)8:print(密码太短至少需要 8 个字符)else:has_upperFalsehas_lowerFalsehas_digitFalsehas_specialFalseforcharinpassword:ifchar.isupper():has_upperTrueelifchar.islower():has_lowerTrueelifchar.isdigit():has_digitTrueelifnotchar.isalnum():has_specialTruescoresum([has_upper,has_lower,has_digit,has_special])ifscore4:strength非常强elifscore3:strength强elifscore2:strength中等else:strength弱print(f密码强度{strength})进阶练习练习 12打印日历importcalendar yearint(input(年份))monthint(input(月份))print(f\n{year}年{month}月的日历)print(calendar.month(year,month))练习 13九九乘法表print(九九乘法表\n)foriinrange(1,10):forjinrange(1,i1):print(f{j}x{i}{i*j:2},end )print()练习 14求阶乘nint(input(输入一个数字))factorial1foriinrange(1,n1):factorial*iprint(f{n}! {factorial})本节小结本节我们通过实战练习巩固了分支结构的各种使用场景掌握了循环的常见算法实现开发了简单的游戏程序学习了数据处理的技巧下一节我们将学习 Python 的第一个数据结构——列表。继续学习列表详解上创建与基本操作