A. Course content
B. Knowledge point
C. Basic instructions used
D. Function return value
E. Write exchange rate converter program
A. Course contentThis lesson mainly focuses on the return value of the function returm, Through learning to write an exchange rate converter program .
B. Knowledge point(1) Defined function
(2) Call function
(3) Return value
C. Basic instructions used(1)def
(2)return
(3)input
(4)print()
D. Function return valueThe return value of a function refers to the value returned after the function is executed . Why should there be a return value ? Because after this function operation is completed , Its results will be used in the following program . Return values allow you to transfer most of the heavy work of the program to functions , To simplify the process .
In the function , have access to return Statement to return the value to the line of code that called the function ,return Is the end of a function , There can be more than one in a function return, But just do it once , The whole function will end . Here's the picture :
Each function has a return value , If the return value is not specified in the function , stay Python After the function execution is completed , By default, a None. Functions can also have multiple return values , If there are multiple return values , Will put all the returned values into a tuple , A tuple is returned .
E. Write exchange rate converter programThis example will be implemented in combination with the return value of the function . We use the formula to calculate the exchange rate in the function , Then let the function return the result of the calculation , As shown in the figure below :
(1) Defined function rate(),x and y For its two formal parameters .x The received argument is the number of currencies ,y The received argument is the exchange rate value .
(2) Put the variable c The value of is returned to the function call line .
(3) Defined function currency(),cur For its formal parameters .cur The received argument is currency type .
(4) use if-else Conditional statements determine which currency the user enters . Just create a new variable r, Assign the exchange rate of the corresponding currency to the variable r.
(5) Put the variable r The value of is returned to the function call line .
(6) New variable currency1, Assign the value entered by the user to the variable currency1.
(7) New variable money, Assign the value of money entered by the user to the variable money.int() Function converts the amount of money entered by the user into an integer .
(8) New variable currency2, Call function currency(), Assign the returned value to the variable currency2. The argument when calling the function is the currency type entered by the user .
(9) New variable rmb, Call function rate(), Assign the returned value to the variable rmb. The arguments when calling the function are the amount of money entered by the user and currency2 Value .
That's all python return Implementation of the exchange rate converter tutorial example details , More about python return For information about exchange rate converter, please pay attention to other relevant articles on software development network !