python语言程序设计-北京理工大学-嵩天等课件代码整理
Posted zhenshj
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python语言程序设计-北京理工大学-嵩天等课件代码整理相关的知识,希望对你有一定的参考价值。
- #TempConvert.py
- TempStr = input("请输入带有符号的温度值: ")
- if TempStr[-1] in [‘F‘, ‘f‘]:
- C = (eval(TempStr[0:-1]) - 32)/1.8
- print("转换后的温度是{:.2f}C".format(C))
- elif TempStr[-1] in [‘C‘, ‘c‘]:
- F = 1.8*eval(TempStr[0:-1]) + 32
- print("转换后的温度是{:.2f}F".format(F))
- else:
- print("输入格式错误")
- #PythonDraw.py
- import turtle
- turtle.setup(650, 350, 200, 200)
- turtle.penup()
- turtle.fd(-250)
- turtle.pendown()
- turtle.pensize(25)
- turtle.pencolor("purple")
- turtle.seth(-40)
- for i in range(4):
- turtle.circle(40, 80)
- turtle.circle(-40, 80)
- turtle.circle(40, 80/2)
- turtle.fd(40)
- turtle.circle(16, 180)
- turtle.fd(40 * 2/3)
- turtle.done()
-
天天向上的力量 第一问
- #DayDayUpQ1.py
- dayup = pow(1.001, 365)
- daydown = pow(0.999, 365)
- print("向上:{:.2f},向下:{:.2f}".format(dayup, daydown))
天天向上的力量 第二问
- #DayDayUpQ2.py
- dayfactor = 0.005
- dayup = pow(1+dayfactor, 365)
- daydown = pow(1-dayfactor, 365)
- print("向上:{:.2f},向下:{:.2f}".format(dayup, daydown))
天天向上的力量 第三问
- #DayDayUpQ3.py
- dayup = 1.0
- dayfactor = 0.01
- for i in range(365):
- if i % 7 in [6,0]:
- dayup = dayup*(1-dayfactor)
- else:
- dayup = dayup*(1+dayfactor)
- print("工作日的力量:{:.2f} ".format(dayup))
天天向上的力量 第四问
- #DayDayUpQ4.py
- def dayUP(df):
- dayup = 1
- for i in range(365):
- if i % 7 in [6,0]:
- dayup = dayup*(1 - 0.01)
- else:
- dayup = dayup*(1 + df)
- return dayup
- dayfactor = 0.01
- while dayUP(dayfactor) < 37.78:
- dayfactor += 0.001
- print("工作日的努力参数是:{:.3f} ".format(dayfactor))
文本进度条 简单的开始
- #TextProBarV1.py
- import time
- scale = 10
- print("------执行开始------")
- for i in range(scale+1):
- a = ‘*‘ * i
- b = ‘.‘ * (scale - i)
- c = (i/scale)*100
- print("{:^3.0f}%[{}->{}]".format(c,a,b))
- time.sleep(0.1)
- print("------执行结束------")
文本进度条 单行动态刷新
- #TextProBarV2.py
- import time
- for i in range(101):
- print("\r{:3}%".format(i), end="")
- time.sleep(0.1)
文本进度条 实例完整效果
- #TextProBarV3.py
- import time
- scale = 50
- print("执行开始".center(scale//2, "-"))
- start = time.perf_counter()
- for i in range(scale+1):
- a = ‘*‘ * i
- b = ‘.‘ * (scale - i)
- c = (i/scale)*100
- dur = time.perf_counter() - start
- print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,dur),end=‘‘)
- time.sleep(0.1)
- print("\n"+"执行结束".center(scale//2,‘-‘))
版本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))
以上是关于python语言程序设计-北京理工大学-嵩天等课件代码整理的主要内容,如果未能解决你的问题,请参考以下文章