First code:
# -*- coding:gb2312 -*-
def get_wendu():
wendu = 22
print("您输入的温度是%d"%wendu)
return wendu
def get_wendu_huashi():
wendu = wendu + 3
print("您输入的新温度是%d"%wendu)
print("------1-------")
wendu = get_wendu()
print("------2-------")
get_wendu_huashi()
The first code execution result:
Second code:
# -*- coding:gb2312 -*-
def get_wendu():
wendu = 22
print("您输入的温度是%d"%wendu)
return wendu
def get_wendu_huashi():
result = wendu + 3 #这里是对第一段代码的修正,把前面一个wendu改成了新的变量名称result
print("您输入的新温度是%d"%result)
print("------1-------")
wendu = get_wendu()
print("------2-------")
get_wendu_huashi()
The second code execution result:
I have two questions:
The first question:
Why is the statement wendu = get_wendu() executed? The result is: the temperature you entered is 22. Isn't this just an assignment statement? Similar to wendu = 22, such an assignment will not print out. Why does wendu = get_wendu() print out the result? Could it be that the print statement in the function will print out this thing during the assignment process?
Second question:
Why is wendu = wendu 3 wrong in the first piece of code, but can it be executed successfully if it is replaced by result = wendu 3? Is it because when the sentence wendu = wendu 3 is executed, the system encounters print("The new temperature you entered is %d"%wendu), and the system cannot tell whether wendu is the previous wendu or the subsequent wendu?
Your first question is actually that you don’t understand the execution of the statement. Wendu = get_wendu(), get_wendu() is an expression, it will return a value, and this value will be assigned to the wendu variable, and get_wendu () This expression is an execution function. It will execute the statements defined in your function body in sequence. If you write print in it, it will naturally execute print.
Your second problem is actually that you don’t understand the difference between local variables and global variables. Printing has no impact at all, it is just caused by different variable references.
First, let’s talk about the statements you can run here.
Here, the variable wendu is not declared and defined inside the function, but it can be used directly. In fact, it refers to the global variable wendu, which is the variable defined here
wendu = get_wendu()
. This also explains why the result printed is 22+3And you can’t run the statement
A new variable wendu is actually declared in the internal scope of the function, which has not yet been defined. Then the wendu variable in wendu+3 refers to this undefined new variable, which naturally causes an error.
Understand the global scope and local scope and you will know where the mistake is.
get_wendu()
is a function call, callingget_wendu
,get_wendu
insideprint("The temperature you entered is %d"%wendu)
is to printwendu
the value of this local variablewendu = wendu + 3
is because the variablewendu
has not been defined yet, and you are using it. It can be seen from the error message,UnboundLocalError: local variable 'wendu' referenced before assignment
print("The temperature you entered is %d"%wendu)
Isn't this sentence just printingPython will search for variables in the current scope by default. Since there is no
wendu
variable in the current scope, an error will naturally be reported. You can addnonlocal wendu
beforewendu = wendu + 3
, so that it can run normallyprint() function prints to the standard output,
The error message is
Local variable 'wendu' referenced before assignment
, which means that the wendu variable was not used before the call. I believe you will be shocked if you translate this Error message. The name is declared in wendu = get_wendu() but it says no, but the computer is not as low-powered as you think = =, = is used for assignment, it must Know which wendu is.The reason is that for variable assignment inside a function, Python will consider this variable to be a local variable, so your wendu is a local variable at this time and not global
You can try to modify it like this
Don’t use Pinyin for variable names, don’t use Pinyin for variable names, don’t use Pinyin for variable names. Use global variables as little as possible. According to your requirements, it will be better to choose to pass parameters.