Python语言程序设计

[第4周]程序的控制-Python语言程序设计(学习笔记)

2018-04-15  本文已影响104人  努力奋斗的durian

文章原创,最近更新:2018-04-15

1.课程导学
2.程序的分支结构
3.实例5:身体质量指数BMI
4.程序的循环结构
5.模块3:random库的使用
6.实例6:圆周率的计算
7.所有的代码汇总
原链接 语言程序设计北京理工大学

1.课程导学

1.1前课复习

1.2本课概要

1.3练习与作业

2.程序的分支结构

2.1单元开篇

2.2单分支结构


类似如果则..如果天气好,出去玩,否则不出去.

2.3二分支结构

2.4多分支结构


这一段代码逻辑有关系,需要进一步的修改.


2.5条件判断及组合

guess =eval(input())
if guess >99 or guess<99:
    print("猜错了")
else:
    print("猜对了")

2.6程序的异常处理

2.7单元小结

3.实例5:身体质量指数BMI

3.1"身体质量指数BMI"问题分析

3.2"身体质量指数BMI"实例讲解

height,weight =eval(input("请输入身高(米)和体重(公斤)[用逗号隔开]:"))
bmi=weight/pow(height,2)
print("BMI的数值为:{:.2f}".format(bmi))
who=""
if bmi <18.5:
    who="偏瘦"
elif 18.5<=bmi<25:
    who="正常"
elif 25<=bmi<30:
    who="偏胖"
elif bmi>=30:
    who="肥胖"
print("IBM为国外指标为{0}".format(who))
height,weight =eval(input("请输入身高(米)和体重(公斤)[用逗号隔开]:"))
bmi=weight/pow(height,2)
print("BMI的数值为:{:.2f}".format(bmi))
nat=""
if bmi <18.5:
    nat="偏瘦"
elif 18.5<=bmi<24:
    nat="正常"
elif 24<=bmi<28:
    nat="偏胖"
elif bmi>=28:
    nat="肥胖"
print("IBM为国内指标为{0}".format(nat))
height,weight =eval(input("请输入身高(米)和体重(公斤)[用逗号隔开]:"))
bmi=weight/pow(height,2)
print("BMI的数值为:{:.2f}".format(bmi))
who,nat="",""
if bmi <18.5:
    who,nat="偏瘦","偏瘦"
elif 18.5<=bmi<24:
    who,nat="正常","正常"
elif 24<=bmi<25:
    who,nat="正常","偏胖"
elif 25<=bmi<28:
    who,nat="偏胖","偏胖"
elif 28<=bmi<30:
    who,nat="偏胖","肥胖"  
elif bmi>=28:
    nat="肥胖"
print("IBM为国外为{0},国内为:{1}".format(who,nat))

3.2"身体质量指数BMI"举一反三

4.程序的循环结构

4.1单元开篇

4.2遍历循环

4.3无限循环

4.4循环控制保留字

4.5循环的高级用法

4.6单元小结

5.模块3:random库的使用

5.1random库的基本介绍

5.2基本随机数函数


程序需要设计再现随机需求就需要设定种子,如果不需要就不用设定种子.只要设置了种子,产生的随机数也相同.

5.3扩展随机数函数


用3-4个随机函数,则随机函数的能力就增加了.

6.实例6:圆周率的计算

6.1"圆周率的计算"问题分析

6.2"圆周率的计算"实例讲解


为了使代码读起来有可读性,可以在代码后面增加 \ 进行换行,换行后的代码与一行的代码是一样的.


pi=0
N=100
for k in range(N):
    pi=pi+1/pow(16,k)*( \
        4/(8*k+1)-2/(8*k+4)- \
        1/(8*k+5)-1/(8*k+6))
print("圆周率值是:{}".format(pi))
from random import random
from time import perf_counter

DARTS=1000*1000
hits=0.0
start=perf_counter()
for i in range(1,DARTS+1):
    x,y=random(),random()
    dist=pow(x**2+y**2,0.5)
    if dist <=1.0:
        hits=hits+1
        
pi=4*(hits/DARTS)
print("圆周率值是:{}".format(pi))
print("运行时间是:{:.5f}s".format(perf_counter()-start))

运行结果是:


6.3"圆周率的计算"举一反三

7.所有的代码汇总

版本1

#CalBMIv1.py
height, weight = eval(input("请输入身高(米)和体重(公斤)[逗号隔开]: "))
bmi = weight / pow(height, 2)
print("BMI 数值为:{:.2f}".format(bmi))
who = ""
if bmi < 18.5:
    who = "偏瘦"
elif 18.5 <= bmi < 25:
    who = "正常"
elif 25 <= bmi < 30:
    who = "偏胖"
else:
    who = "肥胖"
print("BMI 指标为:国际'{0}'".format(who))

版本2

#CalBMIv2.py
height, weight = eval(input("请输入身高(米)和体重\(公斤)[逗号隔开]: "))
bmi = weight / pow(height, 2)
print("BMI 数值为:{:.2f}".format(bmi))
nat = ""
if bmi < 18.5:
    nat = "偏瘦"
elif 18.5 <= bmi < 24:
    nat = "正常"
elif 24 <= bmi < 28:
    nat = "偏胖"
else:
    nat = "肥胖"
print("BMI 指标为:国内'{0}'".format(nat))

版本3

#CalBMIv3.py
height, weight = eval(input("请输入身高(米)和体重(公斤)[逗号隔开]: "))
bmi = weight / pow(height, 2)
print("BMI 数值为:{:.2f}".format(bmi))
who, nat = "", ""
if bmi < 18.5:
    who, nat = "偏瘦", "偏瘦"
elif 18.5 <= bmi < 24:
    who, nat = "正常", "正常"
elif 24 <= bmi < 25:
    who, nat = "正常", "偏胖"
elif 25 <= bmi < 28:
    who, nat = "偏胖", "偏胖"
elif 28 <= bmi < 30:
    who, nat = "偏胖", "肥胖"
else:
    who, nat = "肥胖", "肥胖"
print("BMI 指标为:国际'{0}', 国内'{1}'".format(who, nat))

公式法

#CalPiV1.py
pi = 0
N = 100
for k in range(N):
    pi += 1/pow(16,k)*( \
              4/(8*k+1) - 2/(8*k+4) - \
              1/(8*k+5) - 1/(8*k+6) ) 
print("圆周率值是: {}".format(pi))

蒙特卡罗方法

#CalPiV2.py
from random import random
from time import perf_counter
DARTS = 1000*1000
hits = 0.0
start = perf_counter()
for i in range(1, DARTS+1):
    x, y = random(), random()
    dist = pow(x ** 2 + y ** 2, 0.5)
    if dist <= 1.0:
        hits = hits + 1
pi = 4 * (hits/DARTS)
print("圆周率值是: {}".format(pi))
print("运行时间是: {:.5f}s".format(perf_counter() - start))
上一篇下一篇

猜你喜欢

热点阅读