zoukankan      html  css  js  c++  java
  • Python语言程序设计基础(4)—— 程序的控制结构

    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("
    正常结束")

    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))

  • 相关阅读:
    收听网络状态广播
    常用工具类
    BroadcastReceiver study
    NIO2
    ip route,ip rule, iptables和docker的端口映射
    Hystrix使用小结
    mysql CPU占用高
    mysql隔离级别与锁,接口并发响应速度的关系(2)
    TOMCAT调优内容
    jvm 锁Lock
  • 原文地址:https://www.cnblogs.com/TreeDream/p/9811930.html
Copyright © 2011-2022 走看看