if, while, for process control

if, while, for process control

I. if syntax

  • Just one condition
age_of_girl = 18
if age_of_girl < 25:
    print("Cute girl")
  • Two conditions
age_of_girl = 18
if age_of_girl > 30:
    print("Good aunt")
else:
    print("Cute girl")
  • Multiple conditions
'''
yafeng---->>Super administrator
egon----->>General administrator
Sean------>>Business Supervisor
//Other general users
'''

name = input('Please enter user name: ').strip()

if name == 'yafeng':
    print('Super administrator')
elif name == 'egon':
    print('General administrator')
elif name == 'sean':
    print('Business Supervisor')
else:
    print('Ordinary users')
  • Small exercise
 """
//Analog authentication function:
    1,Receive user input
    2,Judge the user's input results
    3,Return data
"""
from_db_username = 'yafeng'
from_db_password = '666'
username = input("please input your username>>:")
password = input("please input your password>>:")

if username == from_db_username and password == from_db_password:
    print('Login successfully')
else:
    print("Login failed")

II. Cycle structure

  • while cycle

    There are two loop mechanisms in python, while and for. While is also called conditional loop

    We need to pay attention to the difference between continue and break

    Continue: skip this cycle and execute the next cycle, so once there is a continue, no matter how many lines of code there are in the current level, continue will not execute

    break: end the loop of this layer and simply refer to the current while

  • while case list

"""
//Analog authentication function:
    1,Receive user input
    2,Judge the user's input results
    //If you fail to input three times, lock the account
    //If the user logs in successfully:
        //Execution instruction
    3,Return data
"""
from_db_username = 'yafeng'
from_db_password = '666'
count = 0
tag = True

while tag:
    name = input('input your name: ')
    pwd = input('input your password: ')
    if name == from_db_username and pwd == from_db_password:
        print("Landing successfully")
        while tag:
            cmd = input(">>>:")
            if cmd == "exit":
                tag = False
            else:
                print(f"implement{cmd}command")
    else:
        print("Landing failed")
        count += 1
    if count == 3:
        print("Lock accounts")
        tag = False
  • for cycle

    for: provides us with a value taking method independent of index

    Grammatical structure:
    for variable in container type:

    If there are several values in the container object, he will cycle them several times

    You also need to understand the usage of a range()

    For example, range (10) is equivalent to [0,1,2,3,4,5,6,7,8,9]

    range(1,10) is equivalent to [1,2,3,4,5,6,7,8,9]

  • Multiplication table of for cycle

    for i in range(1,10):#Get a 0-9 line
        for j in range(1,i+1):  #It has to be i+1 because range looks after the head and ignores the tail
            print(f"{i}x{j}={i * j}\t", end=" ")
            #\Tput a tab on the console, align vertically
        print()

Tags: Python supervisor

Posted on Mon, 04 Nov 2019 07:03:45 -0800 by htcilt