Python novice tutorial 5, functions

5.1 Create function

In Python, to define a function, use the def statement to write the function name, parentheses, parameters and a gross colon [:], and then write the function body in an indented block.

def hello():
    print("大家好,我是***")

The meaning of this code is: we define a function called [hello()], what this function does is to print out the words'Hello everyone, I am ***'.

5.2 Call function

To call a function, we need to enter the function name and parentheses. If there are no parameters, nothing can be filled in the parentheses. For example, if we want to call the hello() function just now, we can do this

hello()

After running the program, the program will execute the statements inside the function, and the result of running is like this:
 

Python控制台
大家好,我是***

5.3 Functions with parameters

If we define a function like this:
 

def calc(x):
    result = x * x
    print(result)

This is a function with parameters. You need to fill in a parameter in the parentheses of the function, and the function will multiply this by itself and load it into a variable called result, and then print the result.

At this point, if we want to call this function, we need to talk about the parentheses of a function. If there are no parameters, the function will report an error.

For example:

calc(5)
calc(10)

5.4 The return value of the function

The function is like a concert, and the sentences you add to the function are like instructing the workers in the factory how to work. In many cases, we not only need workers to complete the entire work process, but more importantly, we need to let the workers give us the results of production. At this time, we need to use the return statement to return the result of the function to us.

For example, the following function:

def calc(x):
    result = x * x
    print(result)
a = calc(5)
print(a)

The meaning of this code is to create a function, the function receives a parameter, multiplies the parameter with itself, and then loads it into a variable called result, then prints out the result, and then loads the result of the calling function into the variable a Kind, and then print out a.

If after running the program, 25 is displayed, indicating that the function is running normally, but only a None is displayed in the terminal area. This is because we did not use the return statement, so the function did not produce a result, and naturally it was not loaded into the variable a. At this point, we add a code, like this:

def calc(x):
    result = x * x
    print(result)
    return result
a = calc(5)
print(a)

In this way, we can use the result of the function.

have to be aware of is:

When the return statement is executed, it means that the function has obtained the desired result, and the function will stop running at this time

For example:

def calc(x):    
    result = x * x
    print(result)
a = calc(5)
print(a)

In this example, the result of our operation is this:

Python控制台
大家好,我是***

The function will be; Hello everyone, I am ***" After printing out, the result of 1 is returned, so the following code is not executed.

5.5 Small test

1. Please create a function called count. This function has another parameter n. What the function does is to add 100 to n and load the value of m into the variable m, and finally call this function, and see n = What kind of result will be printed at 100

 

2. Please load the result of count(100) into the variable a, and print a + 10 in the terminal area. Tip: To get m, you need to use the return statement to use m as the return value of the function

The correct answer will be announced in the next issue

Answer from the previous issue:

import time
for i in range(10):
    monkey.move(5)
    time.sleep(0.5)
count = 1
while count < 10:
    monkey.move(5)
    time.sleep(0.5)
    count += 1

 

Guess you like

Origin blog.csdn.net/m0_52519239/article/details/113104148