美文网首页
Python基础学习Day2-条件循环

Python基础学习Day2-条件循环

作者: 怕热的波波 | 来源:发表于2020-07-23 21:15 被阅读0次

    收获

    今天学习的内容刚好是之前学习的薄弱部分,一直不太理解循环语句的程序,通过今天的学习,大概了解了整个过程,后面会在实际应用中把今天学习的用上,达到学以致用的效果。

    1. 条件语句

    • if else
    • if elif else
    • assert
    import numpy as np
    
    num = np.random.randint(100)
    guess = input("猜数字0~99:")
    guess = int(guess)
    if guess==num:
        print('Correct')
    else:
        print('Wrong, the num is %s'%num)
    
    #猜数字0~99:60
    #Wrong, the num is 1
    

    assert,模块运行前先检查是否符合先决条件

    num = np.random.randint(100)
    guess = int(input("猜数字0~99:"))
    assert 0 <= guess <=99, "范围错了!"
    if guess==num:
        print('Correct')
    else:
        print('Wrong, the num is %s'%num)
    #猜数字0~99:100
    #AssertionError: 范围错了!
    

    2.循环语句

    • while
    • while else break
    • for
    • while是布尔循环,for是迭代循环
    num = np.random.randint(100)
    n = 0
    while n <7:
        guess = int(input("猜数字0~99:"))
        assert 0 <= guess <=99, "范围错了!"
        if guess==num:
            print('Correct!')
            break
        elif guess>num:
            print('Lower!')
            n += 1
        else:
            print('Higher!')
            n += 1
    print('游戏结束,猜了%s次'%n)
    
    '''
    猜数字0~99:50
    Higher!
    猜数字0~99:75
    Higher!
    猜数字0~99:87
    Higher!
    猜数字0~99:96
    Lower!
    猜数字0~99:93
    Lower!
    猜数字0~99:91
    Lower!
    猜数字0~99:88
    Correct!
    游戏结束,猜了6次
    '''
    

    for循环用于字典

    dict = {'a':3, 'b':4, 'c':5, 'd':2}
    for key in dict.keys():
        print(key)
    print('----------')
    for key, value in dict.items():
        print(key, value)
    print('----------')
    for value in dict.values():
        print(value)
    '''
    a
    b
    c
    d
    ----------
    a 3
    b 4
    c 5
    d 2
    ----------
    3
    4
    5
    2
    '''
    

    for循环与else合用,会先把for循环执行完

    for num in range(10,20):
       for i in range(2, num):
           if num % i == 0:
               j = num/i
               print('%s = %s * %s'%(num, i ,j))
               break
       else:
           print('%s是一个质数'%num)
    '''
    10 = 2 * 5.0
    11是一个质数
    12 = 2 * 6.0
    13是一个质数
    14 = 2 * 7.0
    15 = 3 * 5.0
    16 = 2 * 8.0
    17是一个质数
    18 = 2 * 9.0
    19是一个质数
    '''
    

    enumerate()

    alphabet = 'abcdefghijk'
    for i, letter in enumerate(alphabet,1):
        print(i,letter)
    '''
    1 a
    2 b
    3 c
    4 d
    5 e
    6 f
    7 g
    8 h
    9 i
    10 j
    11 k
    '''
    

    continue 停止本轮循环并开始下轮,break是终止整个循环

    for i in range(10):
        if i % 2 != 0:
            print(i)
            continue
        i += 2
        print(i)
    '''
    2
    1
    4
    3
    6
    5
    8
    7
    10
    9
    '''
    

    占位语句pass

    def new_func():
        pass
    

    Python Comprehensions

    x = [i for i in range(1000,1100) if (i%2) != 0 and (i%3) == 0]
    print(x)
    #[1005, 1011, 1017, 1023, 1029, 1035, 1041, 1047, 1053, 1059, 1065, 1071, 1077, 1083, 1089, 1095]
    
    x = [[i, j] for i in range(3) for j in range(3)]
    print(x)
    #[[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]
    
    dict = {i:(i%2)==0 for i in range(10)}
    print(dict)
    #{0: True, 1: False, 2: True, 3: False, 4: True, 5: False, 6: True, 7: False, 8: True, 9: False}
    

    综合示例

    passwd = ['123', '456', '789']
    valid = False
    count = 3
    while count>0:
        num = input('Input password:')
        for item in passwd:
            if num == item:
                valid = True
                break
        if valid != True:
            print('Invalid input')
            count -= 1
        else:
            break
    '''
    Input password:245
    Invalid input
    Input password:123
    '''
    

    练习题

    1、编写一个Python程序来查找那些既可以被7整除又可以被5整除的数字,介于1500和2700之间。

    x = [i for i in range(1500,2701) if i%7==0 and i%5==0]
    list1 = [[i,j] for i, j in enumerate(x,1)]
    print(list1)
    '''
    [[1, 1505], [2, 1540], [3, 1575], [4, 1610], [5, 1645], [6, 1680], [7, 1715], [8, 1750], [9, 1785], [10, 1820], [11, 1855], [12, 1890], [13, 1925], [14, 1960], [15, 1995], [16, 2030], [17, 2065], [18, 2100], [19, 2135], [20, 2170], [21, 2205], [22, 2240], [23, 2275], [24, 2310], [25, 2345], [26, 2380], [27, 2415], [28, 2450], [29, 2485], [30, 2520], [31, 2555], [32, 2590], [33, 2625], [34, 2660], [35, 2695]]
    '''
    

    2、龟兔赛跑游戏
    补充map(func, nums),函数的映射

    v1, v2, t, s, l = map(int, input().split())
    time, s1, s2 = 0, 0, 0
    while s1<l and s2<l:
        s1 += v1
        s2 += v2
        time += 1
        if s1>=l or s2>=l:
            break
        if s1-s2 >= t:
            s2 += v2*s    
    if s1>s2:
        print('R')
    elif s1<s2:
        print('T')
    else:
        print('D')
    print(time)
    '''
    10 2 5 2 100
    R
    10
    '''
    

    相关文章

      网友评论

          本文标题:Python基础学习Day2-条件循环

          本文链接:https://www.haomeiwen.com/subject/qqkklktx.html