python中int/float计算
练习1:为手机充值
这里用到“+=”运算符
print('当前手机余额为:\033[0;35m8元\033[m')
money=int(input('请输入要充值的金额:'))
money+=8
print('当前手机剩余金额为:\033[0;32m',money,'\033[m元')
执行结果:

练习2:计算当天消耗的卡路里
#计算当天消耗的卡路里
num=int(input('请输入当天的步数:'))
calorie=num*28
print(f'您今天消耗的卡路里是{calorie},即{calorie/1000}千卡')
执行结果:

练习3:预测未来子女的身高
因身高不是整数,所以需要转换成float类型
#预测未来子女的身高
father_height=float(input('请输入父亲的身高:'))
mother_height=float(input('请输入母亲的身高:'))
son_height=(father_height+mother_height)*0.54
print('预测小孩的身高为:{}cm'.format(son_height))
执行结果:


浙公网安备 33010602011771号