Three major flow control statements in Python

Insert picture description here

1. Single branch statement (with only one condition)

    #判断当前用户是否为root
    name = input("Name:")
    if name == "root":
        print("当前是root用户")

Insert picture description here

2. Double-branch statement (execute output separately for a condition that is satisfied or not satisfied)

    #判断输入的年龄是否成年
    age = int(input("Age:"))
    if age >=18:
        print("adult")
    else:
        print("not an adult")

Insert picture description here

3. Multi-branch statement (judging two or more conditions)

    #判断主机的类型
    pc_type = input("please input the type of computer(L,W,M):")
    if pc_type =='L':
        print("Linux")
    elif pc_type == 'W':
        print("Windows")
    elif pc_type == 'M':
        print("Mac")
    else:
        print("Unkown type")

Insert picture description here

4. Ternary operator

    #定义max,并打印
    a=1;b=2
    max = a if a>b else b
    print(max)

Insert picture description here

Extension: random module

random.random(): Generate a random floating-point number from 0 to 1
random.uniform(a,b): Generate a random floating-point number within a specified range
random.randint(a,b): Generate a random integer within a specified range
random.choice('abcdef'): Get a random element from the sequence
random.shuffle([1,2,3,4,5,6]): Shuffle the order in the sequence

while loop

  1. The basic format of while

    While condition is satisfied:
    statement 1...
    else:
    statement to be executed after the loop is completed

Case:

    sum=0
    i=1
    while i < 5:
        sum += i
        i += 1
    print(sum)

Insert picture description here

2. Exercise: while loop user login system

    trycount=0
    while trycount < 3:
        print("*****user login system*****")
        username = input("username:")
        password = input("password:")
        if (username == "root" and password == "123"):
            print("login success")
            break
        else:
            trycount += 1
            print("login failed,%d chances remain" %(3-trycount))
    else:
        print("0 chances remains,please try again later")

Insert picture description here

1. range() built-in function

    range(stop)   #0~stop-1
    range(start,stop)   #start~stop-1
    range(start,stop,step)  #step为步长

Case:

    >>> list(range(3))
    [0, 1, 2]
    >>> list(range(4))
    [0, 1, 2, 3]
    >>> list(range(1,7))
    [1, 2, 3, 4, 5, 6]
    >>> list(range(0,5))
    [0, 1, 2, 3, 4]
    >>> list(range(0,7,3))
    [0, 3, 6]
    >>> list(range(0,9,2))
    [0, 2, 4, 6, 8]
    >>> list(range(0,8,2))
    [0, 2, 4, 6]
    >>> list(range(4,1,-1))
    [4, 3, 2]

    range(3)  =  [0,1,2]             range(6)  =  [0,1,2,3,4,5]
     
    range(1,4)  =  [1,2,3]           range(-3,1)  =  [-3,-2,-1,0]
     
    range(1,6,2)  =  [1,3,5]         range(4,1,-1)  =  [4,3,2]

2. The format of the for loop

    for 变量 in range(10):
            循环需要执行的代码
    else:
         全部循环结束后要执行的代码

3. Exercise 1: Use a for loop to find the sum of 1-100

    #1.求1-100的和
    sum=0
    for i in range(1,101):
        sum += i
    print(sum)

Insert picture description here

4. Exercise 2: Find the sum of 1-100 odd numbers

    sum=0
    for i in range(1,101,2):
        sum += i 
    print(sum)

Insert picture description here

5. Exercise 3: Find the factorial of 10

    sum=1
    for i in range(1,11):
        sum *= i
    print(sum)

Insert picture description here

6. Exercise 4: User login program based on for loop

    要求:
    1.输入用户名和密码 
    2.判断用户名和密码是否正确(‘name==root’,'passwd=‘westos’) 
    3.为了防止暴力破解,登陆次数仅有三次,如果超过三次机会,报错

    for try_count in range(3):
        print("*************user login system***********")
        user = input("please input user name:")
        password = input ("please input password:")
        if (user == "root" and password == "westos"):
            print ("login success")
            break
        else:
            print ("login failed,%d chances remain" %(2-try_count))
    else:
        print("chances gone,please wait 100s to try again")

Insert picture description here

7. Exercise 5: Leap Year Detector

Insert picture description here

8. Exercise 6: 9*9 multiplication formula table

Insert picture description here

1. break statement

The break statement is used to terminate the loop statement, that is, if the loop condition is not False or the sequence has not been completely recursed, it will also stop executing the loop statement.

    for i in range(10):
        if i==5:
            break
        print(i)
    print('westos')         #i=5时跳出它的循环直接执行这一步

Insert picture description here

2.continue statement

continue will only skip the remaining statements in the current loop, and then continue to the next loop.

    for i in range(7):
        if i==5:
            continue
            print('hello')
        print(i)
    print('westos')

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_42958401/article/details/108823405