zoukankan      html  css  js  c++  java
  • Python 基础-3

    使用while打印1 2 3 4  5 6   8 9 10

    count = 0
    #while  count < 10:
    while count < 10:
        count += 1
        if count == 7:
            print('')
        else:
             print(count)
    测试
    1
    2
    3
    4
    5
    6
    
    8
    9
    10
    

      使用while打印1 2 3 4 5 6 8 9 10;当count=7时不打印空格

    while count < 10:
        count += 1
        if count == 7:
            continue 返回重新开始
        else:
             print(count)
    执行
    1
    2
    3
    4
    5
    6
    8
    9
    10
    

      使用while打印1 2 3 4 5 6 8 9 10;当count=7时不打印空格,用pass

    count = 0
    #while  count < 10:
    while count < 10:
        count += 1
        if count == 7:
            pass   跳过
        else:
             print(count)
    执行测试
    
    1
    2
    3
    4
    5
    6
    8
    9
    10
    

      使用while打印100以内的奇数

    count = 1
    while count < 101:
        print(count)
        count += 2
    测试
    1
    3
    5
    7
    9
    11
    13
    15
    17
    19
    21
    23
    25
    27
    29
    31
    33
    35
    37
    39
    41
    43
    45
    47
    49
    51
    53
    55
    57
    59
    61
    63
    65
    67
    69
    71
    73
    75
    77
    79
    81
    83
    85
    87
    89
    91
    93
    95
    97
    99
    

      打印 1-2+3-4+5....99打印结果

    [root@es-3 ~]# cat test.py 
    sun = 0
    count = 1
    while count < 100:
    	if count % 2 == 0:
    		sun = sun - count
    	else:
    		sun = sun + count
    	count += 1
    print(sun)
    [root@es-3 ~]# python3 test.py 
    50
    

      格式化输出

    name = input('请输入名字')
    age = input('请输入年龄')
    heigeht = input('请入身高')
    msg = "我叫%s,今年%s,身高%s" %(name,age,heigeht)   占位符按顺序替换
    print(msg)
    [root@es-3 ~]# python3 test.py 
    请输入名字cd
    请输入年龄98
    请入身高90
    我叫cd,今年98,身高90
    

      格式化输出二  只有%s 和%d

    name = input('请输入名字')
    age = input('请输入年龄')
    job = input('请输入工作')
    hobbie = input('你的爱好')
    
    msg = '''------------cx-------------
    Name   :   %s
    Age    :   %d      数字类型
    job    :   %s
    Hobbie :   %s
    ---------------end----------------''' %(name,int(age),job,hobbie)
    print(msg)
    [root@es-3 ~]# python3 test.py 
    请输入名字cd
    请输入年龄13
    请输入工作erty
    你的爱好yujnk
    ------------cx-------------
    Name   :   cd
    Age    :   13
    job    :   erty
    Hobbie :   yujnk
    ---------------end----------------
    

       格式化输出%

    name = input('请输入姓名:')
    age = input('请输入年龄:')
    height = input('请输入身高:')
    msg = "我叫%s,今年%d,身高%s,学习进度为3%%"  %(name,int(age),height)
    print(msg)
    测试
    [root@es-3 ~]# python3 test.py 
    请输入姓名:cx
    请输入年龄:19
    请输入身高:190 
    我叫cx,今年19,身高190,学习进度为3%
    

       pass 与break在while循环中的区别;当while循环被break打断,就不会执行else的语句了

    count = 0
    while count < 10:
        count += 1
        if count == 3:break   使用break直接中断后续操作
        print (count)
    else:
        print (循环完成)
    测试
    C:UserszrdAppDataLocalProgramsPythonPython37python.exe G:/python/v/rt.py
    1
    2
    
    count = 0
    while count < 10:
        count += 1
        if count == 3:pass 结束本次判断
        print (count)
    else:
            print ("循环完成")
    
    C:UserszrdAppDataLocalProgramsPythonPython37python.exe G:/python/v/test-2.py
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    循环完成
    

      

    草都可以从石头缝隙中长出来更可况你呢
  • 相关阅读:
    21.Merge Two Sorted Lists 、23. Merge k Sorted Lists
    34. Find First and Last Position of Element in Sorted Array
    leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、301. Remove Invalid Parentheses
    31. Next Permutation
    17. Letter Combinations of a Phone Number
    android 常见分辨率(mdpi、hdpi 、xhdpi、xxhdpi )及屏幕适配注意事项
    oc 异常处理
    oc 类型判断
    oc Delegate
    oc 协议
  • 原文地址:https://www.cnblogs.com/rdchenxi/p/10811850.html
Copyright © 2011-2022 走看看