search

Home  >  Q&A  >  body text

Python newbie, about function issues

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?

高洛峰高洛峰2726 days ago922

reply all(4)I'll reply

  • PHP中文网

    PHP中文网2017-06-12 09:28:03

    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 here wendu = 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.

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-06-12 09:28:03

    1. get_wendu() is a function call, calling get_wendu, get_wendu inside print("The temperature you entered is %d"%wendu) is to print wendu the value of this local variable

    2. wendu = wendu + 3 is because the variable wendu 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

    reply
    0
  • 阿神

    阿神2017-06-12 09:28:03

    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 no wendu variable in the current scope, an error will naturally be reported. You can add nonlocal wendu before wendu = wendu + 3, so that it can run normally

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-06-12 09:28:03

    1. The

      print() function prints to the standard output,

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

      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.

    reply
    0
  • Cancelreply