前六章补充案例和知识点(一)

案例和一些知识点的补充(一)
参考文献:[1] 张基温 魏士靖.Python开发案例教程[M].北京:清华大学出版社,2019.
[2] 张云和 刘友祝 王硕.Python3.x全栈开发从入门到精通

  • 简单算术计算:
    1.关于floor除(//)并不是简单的取整数部分,而是向下取整 如:-5//3=-2
    2.关于求余(%)先执行floor除法算出整除值,再按照被除数-(整除值*除数)计算
    案例:从今天开始,100天以后是周几:

tod=5
total=100
week=50//7
date=100%7
if tod-date>=0:
a=tod-date
else:a=tod-date+7
print(‘过了%d周,100天后是周%d’%(week,a))

输出:过了7周,100天后是周3

  • 使用简单的内置函数运算
    abs(x)——求绝对值
    complex([real[,imag]])——创建复数
    ……反正很多,不少运算,如开方不能直接使用sqrt()事先引入math
    案例:二进制转十进制
    思路一:字符串转换成list,按照运算法则算
    思路二:直接转换 代码如下:

bin=‘10001’
int_10=int(bin,2)
print(int_10)
输出:17

关于math模块:提供了两个常量对象e和pi
math.floor向下取整 math.ceil向上取整 math.turnc单纯保留整数
%和 math.fmod %向下取,而fmod直接计算

  • 案例:判断年份是否为闰年

print(‘请输入一个年份:’)
a=int(input())
if a%4000 or a%40 and a%100!=0:
print("%d年,该年份为闰年"%a)
else:
print("%d年,该年份不是闰年"%a)

  • 案例:为了评价一个人是否肥胖,1835 年比利时统计学家和数学家凯特勒(Lambert Adolphe Jacques Quetelet,1796—1874),提出一种简便的判定指标BMI(Body Mass Index,身体质量指数)。它的定义如下:
    BMI = 体重(kg)÷身高^2(m)
    例如:70kg÷(1.75×1.75)=22.86
    按照这个计算方法,WHO(The World Health Organization,世界卫生组织)1997年公布了一个判断人肥胖程度的BMI标准。但是,不同的种族情况有些不同。因此,2000年国际肥胖特别工作组又提出一个亚洲人的BMI标准,后来又公布了一个中国参考标准。这些标准见表1.7。
    表。17

print(’’’
welcome-----------------------------------------
1.if you are not an Asia please enter 1
2.if you are an Asia please enter 2
3.if you are a Chinese please enter 3
------------------------------------------------’’’)
#欢迎菜单
nat=int(input()) #国籍

if nat==1: #WHO标准
print(“please enter your weight:(kg)”)
wei=float(input())
print(“please enter your height:(m)”)
hei=float(input())
BMI=round(wei/(hei,hei),1)
if BMI<18.5:
print(‘you are too thin’)
else:
if 18.5<=BMI<24.9:
print(‘you are normal’)
else:
if 25.0<=BMI<29.9:
print(‘you are a little fat’)
else:
if 30.0<=BMI<34.9:
print(‘you are fat’)
else:
if 35.0<=BMI<39.9:
print(‘you are too fat’)
else:
if BMI>=40.0:
print(‘you are supper fat’)

elif nat==2:#亚洲标准
print(“please enter your weight:(kg)”)
wei = float(input())
print(“please enter your height:(m)”)
hei = float(input())
BMI = round(wei / (hei*hei), 1)
if BMI < 18.5:
print(‘you are too thin’)
else:
if 18.5 <= BMI < 22.9:
print(‘you are normal’)
else:
if 23.0 <= BMI < 24.9:
print(‘you are a little fat’)
else:
if 25.0 <= BMI < 29.9:
print(‘you are fat’)
else:
if BMI>=30.0:
print(‘you are too fat’)

elif nat==3:#中国标准
print(“please enter your weight:(kg)”)
wei = float(input())
print(“please enter your height:(m)”)
hei = float(input())
BMI = round(wei / (hei*hei), 1)
if BMI < 18.5:
print(‘you are too thin’)
else:
if 18.5 <= BMI < 23.9:
print(‘you are normal’)
else:
if 24.0 <= BMI < 26.9:
print(‘you are a little fat’)
else:
if 27.0 <= BMI < 29.9:
print(‘you are fat’)
else:
if BMI >= 30.0:
print(‘you are too fat’)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值