当前位置:   article > 正文

python课本课后题答案,python教材课后答案_py求球体的表面积并保留两位描述

py求球体的表面积并保留两位描述

这篇文章主要介绍了python课本课后题答案,具有一定借鉴价值,需要的朋友可以参考下。希望大家阅读完这篇文章后大有收获,下面让小编带着大家一起了解一下。

@书本为Python程序设计与算法基础教程(第二版)-----江红、余青松   编著

第二章、python语言基础

1.编写程序,输入球的半径,计算球的表面积和体积(结果保留两位小数)

  1. #计算圆的表面积和体积:
  2. import math
  3. r = float(input("请输入球的半径:"))
  4. area = 4 * math.pi * r**2
  5. volume = 4/3*math.pi*r**3
  6. print(str.format("球的表面积为:{0:2.2f},体积为:{1:2.2f}",area,volume))
  7. #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  8. 请输入球的半径:666
  9. 球的表面积为:5573889.08,体积为:1237403376.70

2.编写程序,声明函数getValue(b,r,n),根据本金b,年利率r和年数n计算最终收益v

  1. money = int(input("请输入本金:"))
  2. rate = float(input("请输入年利率(<1):"))
  3. years = int(input("请输入年数:"))
  4. def getValue(b,r,n):
  5. return b*(1+r)**n
  6. print(str.format("本金利率和为:{0:2.2f}",getValue(money,rate,years)))
  7. #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  8. 请输入本金:10000
  9. 请输入年利率(<1):0.6
  10. 请输入年数:6
  11. 本金利率和为:167772.16

3. 编写程序,求解一元二次方程x2-10x+16=0

  1. from math import sqrt
  2. x = (10+sqrt(10*10-4*16))/2
  3. y = (10-sqrt(10*10-4*16))/2
  4. print(str.format("x*x-10*x+16=0的解为:{0:2.2f},{1:2.2f}",x,y))
  5. #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  6. x*x-10*x+16=0的解为:8.002.00

4.编写程序,提示输入姓名和出生年份,输出姓名和年龄

  1. import datetime
  2. sName = str(input("请输入您的姓名:"))
  3. birthday = int(input("请输入您的出生年份:"))
  4. age = datetime.date.today().year - birthday
  5. print("您好!{0}。您{1}岁不会c语言直接学python。".format(sName,age))
  6. #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  7. 请输入您的姓名:zgh
  8. 请输入您的出生年份:1999
  9. 您好!zgh。您20岁。
第三章、程序流控制

1.编写程序,计算1=2+3+…+100之和

  1. #1.使用for循环(递增):
  2. total = 0
  3. for i in range(101):
  4. total += i
  5. print(total)
  6. #2.使用求和公式:
  7. >>> (1 + 100) * 100 /2
  8. 5050.0
  9. #3.使用累计迭代器itertools.accumulate:
  10. >>> import itertools
  11. >>> list(itertools.accumulate(range(1, 101)))[99]
  12. 5050

2.编写程序,计算10+9+8+…+1之和

  1. #使用累计迭代器itertools.accumulate:
  2. >>> import itertools
  3. >>> list(itertools.accumulate(range(1,11)))[9]
  4. 55

3.编写程序,计算1+3+5+7+…+99之和

  1. #1.使用for循环(递增):
  2. total = 0
  3. for i in range(1,100,2):
  4. total += i
  5. print(total)
  6. #2.使用累计迭代器itertools.accumulate:
  7. >>> import itertools
  8. >>> list(itertools.accumulate(range(1,100,2)))[49]
  9. 2500

4.编写程序,计算2+4+6+8+…+100之和

  1. #使用累计迭代器itertools.accumulate:
  2. >>> import itertools
  3. >>> x = list(itertools.accumulate(range(2,101,2)))
  4. >>> x[len(x)-1]
  5. 2550

5.编写程序,使用不同的实现方法输出2000~3000的所有闰年

  1. #1.
  2. for y in range(2000,3001):
  3. if((y % 4 == 0 and y % 100 != 0) or y % 400 == 0):
  4. print(y,end = ' ')
  5. #2.使用calendar模块的isleap()函数来判断:
  6. from calendar import isleap
  7. for y in range(2000,3001):
  8. if(isleap(y)):print(y,end = " ")

6.编写程序,计算Sn=1-3+5-7+9-11…

  1. #1.
  2. n = int(input("项数:"))
  3. total = 0
  4. flag = True
  5. for i in range(1,2*n,2):
  6. if(flag):
  7. total += i
  8. flag = False
  9. else:
  10. total -= i
  11. flag = True
  12. print(total)
  13. #2.
  14. n = int(input("项数:"))
  15. total = 0
  16. x = 2
  17. for i in range(1,2*n,2):
  18. total += pow(-1,x)*i
  19. x += 1
  20. print(total)
  21. #>>>>>>>>>>>>>>>>>>>>>>>>>>
  22. 项数:10
  23. -10

7.编写程序,计算Sn=1+1/2+1/3+…

  1. n = int(input("项数:"))
  2. total = 0.0
  3. for i in range(1,n+1):
  4. total += 1/i
  5. print(total)
  6. #>>>>>>>>>>>>>>>>>>>>>>>>>
  7. 项数:10
  8. 2.9289682539682538

8.编写程序,打印九九乘法表。要求输入九九乘法表的各种显示效果(上三角,下三角,矩形块等方式)

  1. #矩形块:
  2. for i in range(1,10):
  3. s = ""
  4. for j in range(1,10):
  5. s += str.format("%d * %d = %02d " %(i, j, i*j))
  6. print(s)
  7. #下三角:
  8. for i in range(1,10):
  9. s = ""
  10. for j in range(1,i+1):
  11. s += str.format("
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/833529
推荐阅读
相关标签
  

闽ICP备14008679号