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

  • 相关阅读:
    登陆跳转回原来页面
    magento获取当前浏览器的url和对url判断处理
    Magento获取购物车所有信息
    magento查询商品库存
    金额转换成0.00的形式
    Magento网站运行原理解析
    linux下如何禁用root账户直接登陆
    web前端常用的10个jQuery特效总结
    js函数
    网站css
  • 原文地址:https://www.cnblogs.com/TreeDream/p/9811930.html
Copyright © 2020-2023  润新知