Python newbie, about function issues
高洛峰
高洛峰 2017-06-12 09:26:03
0
4
818

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?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all (4)
洪涛

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.

def get_wendu_huashi(): result = wendu + 3

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 herewendu = get_wendu(). This also explains why the result printed is 22+3

And you can’t run the statement

def get_wendu_huashi(): wendu = wendu + 3 print("您输入的新温度是%d"%wendu)

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.

    仅有的幸福
    1. get_wendu()is a function call, callingget_wendu,get_wenduinsideprint("The temperature you entered is %d"%wendu)is to printwenduthe value of this local variable

    2. wendu = wendu + 3is because the variablewenduhas not been defined yet, and you are using it. It can be seen from the error message,UnboundLocalError: local variable 'wendu' referenced before assignment

      阿神
      1. print("The temperature you entered is %d"%wendu)Isn't this sentence just printing

      2. Python will search for variables in the current scope by default. Since there is nowenduvariable in the current scope, an error will naturally be reported. You can addnonlocal wendubeforewendu = wendu + 3, so that it can run normally

        仅有的幸福
        1. The

          print() function prints to the standard output,

          print("您输入的温度是%d"%wendu) # print会打印括号中的内容,%d表示格式化输出int类型, # 其实这句话和print(("您输入的温度是22")是等价的
        2. The error message isLocal 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

          def get_wendu_huashi(): global wendu wendu = wendu + 3 print("您输入的新温度是%d"%wendu)
        3. 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.

          Latest Downloads
          More>
          Web Effects
          Website Source Code
          Website Materials
          Front End Template
          About us Disclaimer Sitemap
          php.cn:Public welfare online PHP training,Help PHP learners grow quickly!