Process control theory of py language

Posted by mnewbegin on Mon, 07 Mar 2022 13:26:03 +0100

Process control theory of py language

Process control refers to controlling the development process of things. In python, it means to realize the function of running py program according to a certain sequence and steps. The main process control statements in python generally include: selection statement, loop statement, jump statement, pass statement and so on.

The execution process is classified according to the program structure, including:

  • Sequential structure: as the name suggests, the program is executed from top to bottom according to the Arabic numerals on the left of the program code.
  • Branch structure: it can also be said to be the structure of selection and judgment, that is, start branches and select different processes according to different conditions. Branch structures generally include if branch, elif branch, else branch and so on
  • Cycle structure: that is, a certain program is executed repeatedly under certain conditions. This cycle has the conditions and standards of start and end. The loop structure is generally a while loop structure
  • if branch structure

That is: if is a logical judgment condition, and the condition holds up the sub code to be executed.

age = 18
if age > 30:
    print('middle-aged person')
  • if...else branch structure

That is: if condition holds, execute a sub code; If condition does not hold, else condition holds, and then execute b sub code.

age = 18
if age > 30:
    print('middle-aged person')
else
    print('young people')
  • if...elif...else... Branching structure

That is, when the if condition is established, execute the subcode under the if branch; If the condition is not true but the elif condition is true, execute the subcode under the elif branch; Neither if condition nor elif condition holds; Execute the subcode under the else branch. Among them, if branch, elif branch and else branch can be countless in theory.

username = input('username>>>:')
password = input('password>>>:')
if username == 'jason' and password == '123':
    print('Login successful')
else:
    print('Wrong account and password')
score = input('Please enter your grade>>>:')
if score > '90':
    print('Excellent performance')
elif score > '80':
    print('Good results')
elif score > '70':
    print('Average performance')
elif score > '60':
    print('Pass the grade')
else:
    print('Unqualified results')

note: it should be noted that the if branch structure can be nested, that is, there can also be an if branch structure in a sub code under the if branch.

score = input('Please enter your grade>>>:')
if score > '60':
    if score > '80':
        print('Excellent performance')
    else:
        print('Pass the grade')
else:
    print('Unqualified results')
  • while loop structure

As the name suggests, that is, when the while condition is true, the subcode under the while loop structure can be executed repeatedly until the conditions for loop execution are no longer met.

1. First judge whether the condition is true. If so, execute the loop body code.
2. After the execution of the loop body code, judge whether the condition is true again. If it is true, continue to execute the loop body code. If it is not true, jump out of the loop.


sum = 0
n = 0
while n <= 100:
sum = sum + n
n += 1
print(sum)
Execution result: 1+2+...+100= 5050

 

  • while+break loop structure

Break is used to directly end the loop of this layer. It can only end the loop of the layer where the break is located.

  • while+continue loop structure

continue is used to end the whole while loop or start the next loop. note: directly jump to the place where the cycle condition is judged, start running the program, and make the next condition judgment.

Dependencies in python Programming Language

  • python language uses indentation to express the dependency relationship between codes, that is, the child code should be below the parent code, and generally indent four characters.
  • Not all codes can have subcodes. There are subcodes in the code of conditional branch structure.
  • If multiple lines of code belong to the same parent code, they need to ensure the same indentation.
  • Codes with the same indentation are executed in a sequential structure with no dependency.
  • If the last line of code ends with a colon, the next line of code must be indented, which is the subcode of the previous line of code. The colon can be seen as a sign of whether the next line of code is a subcode.

task

Write an age guessing game.
Basic requirements:
If it's wrong, you can guess three times. If it's right, end it directly.
Elevation exercise:
After three opportunities are used up, the user will be prompted whether to continue guessing. If the user enters y, the user will be given three more opportunities; If the user enters n, it ends directly.

Basics:

number = 1
while number < 4:    # Loop statement, judgment variable number Whether the value of is less than 4. If it is less than 4, execute the subroutine below the circular statement
    age = input('age>>>:')  # Please guess this age
    if age == '20':    # if Logical judgment and check whether it is the correct answer
        print('Congratulations, you guessed right')    # Correct answer, output (congratulations, you guessed right)
        break    # break That is, end the loop of this line, that is, if the answer is correct, stop running the subroutine in the loop statement and directly output the result
    else:    # else Logical judgment, wrong answer, execute the following procedure
        print('I'm sorry. Encourage me')    # Wrong answer, printout (sorry, encourage)
        number += 1    # Incremental assignment, i.e. variable number Value of+1
    continue    # variable number If the value of is not less than 4, that is, the circular statement ends the circular operation, and the program ends here

 

Improve:

number = 1  # Variable naming
while number < 4:  # Loop statement, judgment variable number Whether the value of is less than 4. If it is less than 4, execute the subroutine below the circular statement
    age = input('age>>>:')  # Please guess this age
    if age == '20':  # if Logical judgment and check whether it is the correct answer
        print('Congratulations, you guessed right')  # Correct answer, output (congratulations, you guessed right)
        break  # break That is, end the loop of this line, that is, if the answer is correct, stop running the subroutine in the loop statement and directly output the result
    else:  # else Logical judgment, wrong answer, execute the following procedure
        print('I'm sorry. Encourage me')  # Wrong answer, printout (sorry, encourage)
        number += 1  # Incremental assignment, i.e. variable number Value of+1
    continue  # variable number If the value of is not less than 4, that is, the circular statement ends the circular operation, and the program ends here
while number == 4:  # while Logical judgment number Is it equal to 4
    number_of_time = input('Continue the game>>>:')    # Get the data information of whether to restart the game
    if number_of_time == 'y':    # if Condition judge whether to choose to play again
        number1 = 1  # Name a new variable and start a new cycle
        while number1 < 4:  # Loop statement, judgment variable number1 Whether the value of is less than 4. If it is less than 4, execute the subroutine below the circular statement
            age = input('age>>>:')  # Please guess this age
            if age == '20':  # if Logical judgment and check whether it is the correct answer
                print('Congratulations, you guessed right')  # Correct answer, output (congratulations, you guessed right)
                break  # break That is, end the loop of this line, that is, if the answer is correct, stop running the subroutine in the loop statement and directly output the result
            else:  # else Logical judgment, wrong answer, execute the following procedure
                print('I'm sorry to encourage you')  # Wrong answer, printout (sorry, encourage)
                number1 += 1  # Incremental assignment, i.e. variable number1 Value of+1
        continue  # variable number1 If the value of is not less than 4, that is, the circular statement ends the circular operation, and the program ends here
    elif number_of_time == 'n':  # elif To obtain the information that the game is no longer played
        print('The game is over. Thank you for your participation')