【问题标题】:Need some clearing up with function calls需要对函数调用进行一些清理
【发布时间】:2016-12-15 09:27:17
【问题描述】:

我目前正在为我的考试做一些复习,但我遇到了一个奇怪的问题,或者我可能正在监督一个非常简单的错误,但由于某种原因我无法发现。我正在练习函数和这些函数之间的参数传递,我编写了一个非常简单的程序来确保我理解了基础知识:

 def temperature(celsius, fahrenheit):
      celsius = 15
      fahrenheit = 59
      print("The current temperature in C is: ", celsius)
      print("The current temperature in F is: ", fahrenheit)
      return(celsius, fahrenheit)

 temperature(celsius, fahrenheit)

现在我不确定是否需要返回值,但我把它们放在那里是因为我记得我的教授说这很重要。

现在,问题是当我尝试运行它时,它告诉我应该传递的变量在程序运行时甚至无法识别。有人可以解释为什么这是问题吗?

为了将来参考,如果我想在 2 个或更多函数之间传递这些变量,我将如何做?

更新:添加以下文本以进一步澄清

这是我的教授提供给我的一些代码作为示例。他是如何传递那些局部变量的?

def introduction ():
    print ("""
Celsius to Fahrenheit converter
-------------------------------
This program will convert a given Celsius temperature to an equivalent
Fahrenheit value.

    """)

def display(celsius, fahrenheit):
    print("")
    print("Celsius value: ", celsius)
    print("Fahrenheit value:", fahrenheit)

def convert():
    celsius = float(input("Type in the celsius temperature: "))
    fahrenheit = celsius * (9 / 5) + 32
    display(celsius, fahrenheit)

def start():
    introduction ()
    convert ()

start()

【问题讨论】:

    标签: python function parameters execution


    【解决方案1】:

    您在理解函数的工作原理时遇到了问题……

    函数中定义的变量的范围不超出函数本身。也就是说,如果你退出函数,这些变量(如果它们不是全局的)就不再存在了。

    您应该在调用函数之前定义变量,或者将它们作为关键字参数传递给它们默认值:

    celsius = 15
    fahrenheit = 59
    def temperature(celsius, fahrenheit):
          print("The current temperature in C is: ", celsius)
          print("The current temperature in F is: ", fahrenheit)
          return(celsius, fahrenheit)
    

    然后这样称呼它:

    temperature(celsius, fahrenheit)
    

    或者使用关键字:

    def temperature(celsius=15, fahrenheit=59):
          print("The current temperature in C is: ", celsius)
          print("The current temperature in F is: ", fahrenheit)
          return(celsius, fahrenheit)
    

    这样称呼它:

    temperature() # --> use the default parameters
    #or 
    temperature(celsius=0, fahrenheit=10) --> change the values of your parameters
    

    但是在这两种方式中,都不需要像你所做的那样覆盖函数中的变量。


    更新

    变量celsius的值是用户在控制台输入变量值时设置的:

    celsius = float(input("Type in the celsius temperature: "))
    

    convert() 函数有什么作用?

    def convert():
         celsius = float(input("Type in the celsius temperature: ")) #<-- set the value of the variable to that entered by the user thanks to input()
         fahrenheit = celsius * (9 / 5) + 32 # <-- creates the variable from celsius
         display(celsius, fahrenheit) # <-- calls the display function with celsius and fahrenheit being well defined
    

    input 将用户输入的值作为string,然后将其转换为int

    【讨论】:

    • 如果你不介意的话,我会把它放在帖子的编辑版本中!
    • cf“更新”部分
    • 所以 display 只是显示用户在转换函数中所做的输入。所以我们应该创建新变量并调用我们希望将值“传输”到的函数,因为没有更好的术语?
    • 什么意思:So we should make new variables and call the function we want the values "transported" to
    • 就像在 convert() 中一样,摄氏度和华氏度在函数中被定义为输入。因此,如果我们想在另一个函数中访问这些值,我们是否应该总是在将一个新变量传递给另一个函数之前为其分配一个新变量?对不起,如果我没有意义,我不知道如何正确表达我的东西。
    【解决方案2】:

    在调用 temperature(celsius, fahrenheit) 之前,您需要定义变量 celsius 和 fahrenheit。

    例如:

    def temperature(celsius, fahrenheit):
        celsius = 15
        fahrenheit = 59
        print("The current temperature in C is: ", celsius)
        print("The current temperature in F is: ", fahrenheit)
        return(celsius, fahrenheit)
    
    celsius = 20   
    fahrenheit = 15
    
    temperature(celsius, fahrenheit)
    

    然后就可以了。

    【讨论】:

      【解决方案3】:

      你得到了错误,因为你声明的变量的范围是'celsius'和'farenheit'是函数 temparature() 的本地变量,你应该在函数之外声明它,这样它们的范围就不会受到限制。

       def temperature(celsius, fahrenheit):
        print("The current temperature in C is: ", celsius)
        print("The current temperature in F is: ", fahrenheit)
        return(celsius, fahrenheit)
      
      celsius = 15
      fahrenheit = 59
      temperature(celsius, fahrenheit)
      

      【讨论】:

        猜你喜欢
        • 2013-08-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-11
        • 1970-01-01
        • 2023-03-26
        相关资源
        最近更新 更多