PM2.5
pm = eval(input()) if pm>=75: print("空气存在污染") else : print("空气没有污染") print("空气{}污染".format("存在" if pm>=75 else "没有"))
健康
height,weight = eval(input()) bmi = weight/(height**2) who = "" if bmi < 18.5: who = "偏瘦" elif bmi < 25: who = "正常" elif bmi < 30: who = "偏胖" else: who = "肥胖" print(who)
异常处理
try: num = eval(input()) print(num*2) except "输入错误": print("输入错误了") else: print("没有输入错误") finally: print("程序完成") for c in "TreeDream": print(c,end="") else : print("\n正常结束")
random
from random import * print(random()) print(uniform(1,10)) print(uniform(1,20)) print(randrange(0,15,5)) print(choice(range(100))) ls = list(range(10)) print(ls) shuffle(ls) print(ls) seed(125) print("{}.{}.{}".format(randint(1,10),randint(1,10),randint(1,10))) print("{}.{}.{}".format(randint(1,10),randint(1,10),randint(1,10))) seed(125) print("{}.{}.{}".format(randint(1,10),randint(1,10),randint(1,10)))
pi
import random import math import time DARTS = 1000 hits = 0 #time.clock() for i in range(DARTS+1): x,y = random.random(),random.random() dist = math.sqrt(x**2+y**2) if dist <=1: hits+=1 print("{}".format(4*hits/DARTS))
习题部分
猜数字
import random x = random.randint(0,100) print(x) cnt = 1 g = eval(input()) try: while x != g : cnt+=1 print("{}".format("大" if g > x else "小")) g = eval(input()) print("猜了{}次".format(cnt)) except "input error" : print("输入错误")
gcd
def gcd(a,b): if b == 0: return a else : return gcd(b,a%b) print(gcd(102,9))