博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python3练习题 - 来自菜鸟的独白
阅读量:3725 次
发布时间:2019-05-22

本文共 1465 字,大约阅读时间需要 4 分钟。

1.小明身高1.75,体重80.5kg。请根据BMI公式(体重除以身高的平方)帮小明计算他的BMI指数,并根据BMI指数:

  • 低于18.5:过轻
  • 18.5-25:正常
  • 25-28:过重
  • 28-32:肥胖
  • 高于32:严重肥胖

if-elif判断并打印结果:

height= input('height: ')weight = input('weight: ')w = float(weight)h = float(height)h1 = h/100bim = w/(h1*h1)print(bim)if bim <= 18.5:    print('过轻')elif 18.5 <= bim < 25:    print('正常')elif 25 <= bim <28:    print("过重")elif 28 <= bim < 32:    print("肥胖")else:    print("超级牛")

2.练习

请定义一个函数quadratic(a, b, c),接收3个参数,返回一元二次方程 ax^2+bx+c=0ax2+bx+c=0 的两个解。

提示:

一元二次方程的求根公式为:

x=\frac{-b\pm\sqrt{b^2-4ac}}{2a}x=2a−b±b2−4ac​​

计算平方根可以调用math.sqrt()函数:

>>> import math>>> math.sqrt(2)1.4142135623730951
import mathdef quadratic(a, b, c):    if not isinstance(a, (int, float)):        raise TypeError('bad operand type')    if not isinstance(b, (int, float)):        raise TypeError('bad operand type')    if not isinstance(c, (int, float)):        raise TypeError('bad operand type')    if b * b - 4 * a * c < 0:        print = ('此方程组无解')        return    if a == 0 and b == 0:        print = ('此方程组无解')        return    if a == 0 and b != 0:        x = -c / b        return x    if b * b - 4 * a * c >= 0:        x1 = (-b + math.sqrt(b ** 2 - 4 * a * c)) / (2 * a)        x2 = (-b - math.sqrt(b ** 2 - 4 * a * c)) / (2 * a)    return x1, x2# 测试print('quadratic(2,3,1)=', quadratic(2, 3, 1))print('quadratic(1,3,-4)=', quadratic(1, 3, -4))if quadratic(2, 3, 1) != (-0.5, -1.0):    print('测试失败')if quadratic(1, 3, -4) != (1.0, -4.0):    print('测试失败')

 

转载地址:http://sqtnn.baihongyu.com/

你可能感兴趣的文章
2021-02-13L:1652 && 2.08---2.14&&1576L
查看>>
2021-02-17-----2-20
查看>>
2021-02-21&&和||的本质问题
查看>>
2021-02-22----03-03
查看>>
2021-04-19&&dp回文串
查看>>
2021-3-20——04-19——4.05
查看>>
js零碎知识点链接
查看>>
3.null 与 Object
查看>>
4.对象和非对象之间的相等比较
查看>>
5对象相等问题——以组合使用构造函数模式和原型模式的对象创建为例
查看>>
2021-04-28腾讯面试补充
查看>>
2021-04-30联想面试补充
查看>>
2021-05-03-Vue补充计算属性的缓存
查看>>
2021-05-04/62/回顾1——93回顾2——123回顾3——()——回顾169
查看>>
2021-05-05——94补充:runtime-only和runtime+compiler
查看>>
2021-05-06——Vue补充:$route和$router的区别
查看>>
6.区分数组和对象
查看>>
2021-05-12网易面试补充
查看>>
2.常用排序算法
查看>>
7.forEach()中的break与continue
查看>>