Getting the python syntax Process Control

Process Control

First, the flow control

Process control is the control flow, the specific control flow of the program execution, the execution flow of the program is divided into three structures: sequential structure, a branched structure (if a judgment), the cyclic structure (for use and while)

The main purpose is to make computing to help people work, so programs written judgment is required

The syntax structure : python through indentation to determine the ownership of the code (4 spaces for an indent)

Second, the branch structure

  • if mainly used to judge things right or wrong, true and false, if feasible
if 条件   #如果结果为True,就依次执行:代码1,代码2
    代码1
    代码2
    ……
elif 条件2    #可以加N多个elif
    代码3
    代码4
    ……
else    #条件不成立的情况下,就依次执行:
    代码5
    代码6
  • 0, None, "" an empty string, [] empty list, {} empty dictionary can be used as False
  • Others are True

Example 1:

If: a woman age> 30, then called: aunt, otherwise: Miss

age_of_girl=31
if age_of_girl > 30:
    print('阿姨')
else:
    print('小姐')

Example 2:

If the score> = 90, so good

If the score> = 80, so good

If the score> = 60, then qualified

Otherwise, do not pass

score = input('请输入你的成绩>>>:')
score = int(score)
if score >= 90:
    print('优秀')
elif score >= 80:
    print('良好')
elif score >= 60:
    print('合格')
else:
    print('不及格')

  • if nested

    If: Women 18 <= Age <= 24 and a height greater than 120 and less than 160 weight is pretty

    So tell the truth, otherwise: Excuse me

    If: confession successful, together

    Otherwise: Love your mother to sell cannabis situation

age_of_girl = 22
height = 165
weight = 110
is_pretty = True
success = True
if 18 <= age_of_girl <= 24 and height > 160 and weight < 120 and is_pretty:
    if success:
        print('表白成功,在一起')
    else:
        print('爱你妈卖麻花情')
else:
    print('打扰了')

Log function

Username = input('请输入用户名:') .strip()    # .strip的作用可以过滤掉你输入内容中多加的空格
Password = input('请输入密码:') .strip()
if Username == 'yang' and Password == '123':
    print('登录成功')
else:
    print('用户名或密码错误')

Third, the loop structure

Loop structure is repeatedly performed a certain code block

  • while loop syntax

    There python while and for two kinds of loop mechanism, while loop region called conditional loop

  • while operating steps:

    Step 1: If the condition is true, then the first performance: Code 1, Code 2, ......

    Step 2: determination conditions again after the implementation, if the condition is True again executing :( Code 1, Code 2, ......, if the condition is False, the loop terminates)

while 条件:
    代码1
    代码2
    ……

Example 1: basic use while loop

Three failed user authentication program

Representative break the end of the loop layer, and then continue to the end of the cycle, the cycle time of the direct access

username = 'yang'
password = '123'
count = 0   # 用于记录错误验证的次数
while count < 3:
    inp_name = input('请输入用户名:')
    inp_pwd = input('请输入密码:')
    if inp_name == username and inp_pwd == password:
        print('登录成功')
        #用于结束本层循环
        #break   # 这个是while循环嵌套+break
    else:
        print('输入的用户名或密码错误')
        count += 1

Examples of two: while using a nested loop + tag

username = 'yang'
password = '123'
count = 0   # 用于记录错误验证的次数
tag = True
while tag:
    inp_name = input('请输入用户名:')
    inp_pwd = input('请输入密码:')
    if inp_name == username and inp_pwd == password:
        print('登录成功')
        while tag:
            cmd = input(">>:")
            if cmd == 'exit':
                tag = False     # tag变为False,所有while虚幻的条件都变为False
                break   # 用于结束本层循环
        print('run<%s>' % cmd)
        break   # 用于结束本层循环,即第一层循环
    else:
        print('输入的用户名或密码错误')
        count += 1

Example 3: Use a while and continue

number = 0
while number < 10:
    number += 1
    if number == 7:
        continue    #结束掉本次循环,即本次虚幻continue之后的代码都不会运行了,而是直接进入下一步
    print(number)

Example 3: Use while and else

count = 0
while count <= 5:
    count += 1
    # if count == 3 :
    # break
    print('Loop', count)
else:
    print('循环正常执行完了')

Guess Age

age = 18
count = 0
while count < 3:
    count += 1
    guess = int(input('>>:'))
    if guess > age:
        print('大了')
    elif guess < age:
        print('小了')
    else:
        print('答对了')
  • for loop syntax
    • The second implementation cycle structure is the for loop, a for loop can do things, can be achieved while the reason for loop because the value of the loop for loop is more concise than using a while loop

grammar:

for the variable name in the object: (the object here can be a string / list / dictionary)

Code a

Code two

​ ……

#例如:
for number in [1, 2, 3]:
    print(number)
1
2
3
#打印出下面图形:
*****
*****
*****

for i in range(3):
    for j in range(5):
        print('*', end='')
    print() #代表换行

Print multiplication table:

for i in range(1,10):
    for j in range(1,i+1):
        print('%s*%s=%s ' % (i, j, i*j), end='')
    print()

Print Pyramid:

#分析
'''
#max_level=5
    *       #current_level=1,空格数=4,*号数=1
   ***      #current_level=2,空格数=3,*号数=3
  *****     #current_level=3,空格数=2,*号数=5
 *******    #current_level=4,空格数=1,*号数=7
*********   #current_level=5,空格数=0,*号数=9

#数学表达式
空格数=max_level-current_level
*号数=2*current_level-1
'''
#实现:
max_level = 5
for current_level in range(1, max_level+1):
    for i in range(max_level-current_level):
        print(' ', end='')
    for j in range(current_level * 2 - 1):
        print('*', end='')
    print()

Guess you like

Origin www.cnblogs.com/YGZICO/p/11793989.html