1.猜年龄,连续猜三次,猜对了或不想猜了退出,猜错了判断是否继续
a = 19 GameOver = 0 while 1: for i in range(3, 0, -1): # 从3到1,for循环的逆向输出 print("您还有%d次机会" % i) ii = int(input("请输入你要猜的数字:")) if ii > a: print("猜大了,往小了猜") elif ii < a: print("猜小了,往大了猜") elif ii == a: print("恭喜您答对了,年龄为%d" % a) break if ii==a: break else: print("按任意键结束游戏,按Y或者y继续游戏:") while 1: select = input("请输入你的选择:") if select == 'Y' or select == 'y': print('请您继续') break elif select == 'N' or select == 'n': GameOver = 1 break else: print('输入信息错误,,请重新输入') continue if GameOver == 1: print('游戏结束') break
2.小明身高1.75m,体重80.5kg,计算它的BML指数(体重除以身高的平方)判断他是否健康
a, b = 1.75, 80.5 c = b / (a ** 2) print('他的BML指数为%f' % c) if c < 18.5: print('过轻') elif c >= 18.5 and c < 25: print('正常') elif c >= 25 and c < 28: print('过重') elif c >= 28 and c < 32: print('肥胖') else: print('严重肥胖')
自学的,https://www.bilibili.com/video/BV1vA411b7Rn?p=21
2021.2.1 修改第一份代码,完善了三次循环以后的操作