zoukankan      html  css  js  c++  java
  • python基础:条件循环字符串

    一、

    完成完整的温度转换程序

    while True:
    
        a = int(input('摄氏度转换为华氏温度请按1
    华氏温度转化为摄氏温度请按2
    '))
    
        if a == 1:
    
            celsius = float(input('输入摄氏温度:'))
    
            fahreaheit = (celsius + 1.8) + 32  # f = c+9/5+32
    
            print('{:.2f}摄氏温度转为华氏温度为{:.2f}'.format(celsius, fahreaheit))
    
        elif a == 2:
    
            celsius1 = float(input('输入华氏温度:'))
    
            fahreaheit1 = (celsius1 - 32) * 5 / 9
    
            print('{:.2f}华氏温度转化为摄氏温度为{:.2f}'.format(celsius1, fahreaheit1))
    
        else:
    
            break;
    

     运行结果:

    二、

    猜数字游戏(猜价格,猜年龄等)

    import random
    
    secret = random.randint(1,10)
    
    #print(secret)
    
    print('猜数字游戏')
    
    guess = -1
    
    while guess != secret:
    
        a = input('请输入数字:')
    
        guess = int(a)
    
        if guess > secret:
    
            print('输入的数字太大!')
    
        elif guess < secret :
    
            print('输入的数字太小!')
    
        else :
    
            print('恭喜你,猜对了!')
    

     运行结果:

    三、

    解析身份证号、学号不同片段的含义

    id='500104111111191689'
    
    a="省份编码:"+id[0:2]
    b="地区编码:"+id[2:4]
    c="县区编码:"+id[4:6]
    d="出生年月日;"+id[6:10]+"年"+id[10:12]+"月"+id[12:14]+"日"
    e="户口所在派出所编码:"+id[14:16]
    f="性别编码:"+id[16:17]
    h="校验码:"+id[17:18]
    
    print('该同学身份信息是:
    '+a,b,c,d,e,f,h)
    
    stuid="201606050063"
    print("年级"+stuid[0:4])
    print("专业"+stuid[4:8])
    print("班级"+stuid[8:10])
    print("序号"+stuid[10:])
    

      

    运行结果:

    四、

    字符串的:连接,重复,in判断

    s='hello,'
    t='我是XXX'
    #字符串连接
    print(s+t)
    #字符串重复
    print(s*2)
    print('o' in s)
    

      运行结果:

    五、

    用for产生一系列网址:

    for i in range(0,30,2):  #查询0-30条校园新闻中的偶数网址
        print('http://news.gzcc.cn/html/xiaoyuanxinwen/' + str(i) + '.html')   #方法一
        print('http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i))    #方法二
    

      运行结果:

    六、

    用for循环产生字符串遍历:

    str = 'Hello'
    for i in str:
    print(i)

    运行结果:

    
    
  • 相关阅读:
    给XML文件定义DTD
    详探TextRange对象用execCommand()执行命令
    获取屏幕,浏览器,网页高度宽度
    花点时间搞清top、postop、scrolltop、scrollHeight、offsetHeight
    DataView数据组件
    回调实现无刷新级联下拉框(.net)
    ASP.NET的简单数据绑定
    详探TextRange对象查找与选择
    方维系统常用的jquery库以及各个库的含义
    方维购物分享系统 给店铺品牌加喜欢收藏的功能
  • 原文地址:https://www.cnblogs.com/miehahaha/p/9639628.html
Copyright © 2011-2022 走看看