Python 需要对函数调用进行一些清理吗

Python 需要对函数调用进行一些清理吗,python,function,parameters,execution,Python,Function,Parameters,Execution,我目前正在为考试做复习,我有一个奇怪的问题,或者我可能在监督一个非常简单的错误,因为某种原因我无法抓住。我正在练习在这些函数之间传递函数和参数,我编写了一个非常简单的程序,以确保我理解了基本原理: def temperature(celsius, fahrenheit): celsius = 15 fahrenheit = 59 print("The current temperature in C is: ", celsius) print("

我目前正在为考试做复习,我有一个奇怪的问题,或者我可能在监督一个非常简单的错误,因为某种原因我无法抓住。我正在练习在这些函数之间传递函数和参数,我编写了一个非常简单的程序,以确保我理解了基本原理:

 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)
现在我不确定是否需要返回值,但我把它们放在那里,因为我记得我的教授说这很重要

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

作为将来的参考,如果我想在两个或多个函数之间传递这些变量,我将如何做呢

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

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

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()
在调用温度(摄氏度、华氏度)之前,需要定义变量摄氏度和华氏度

例如:

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)

然后它就可以工作了。

您得到了错误,因为您声明的'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)
您在理解函数如何工作时遇到问题

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

在调用函数之前,您应该定义变量,或者将变量作为关键字参数传递,默认情况下为其赋值:

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)
temperature() # --> use the default parameters
#or 
temperature(celsius=0, fahrenheit=10) --> change the values of your parameters
或使用关键字:

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(celsius, fahrenheit)
temperature() # --> use the default parameters
#or 
temperature(celsius=0, fahrenheit=10) --> change the values of your parameters
但在这两种情况下,都不需要像您所做的那样覆盖函数中的变量


更新

当用户在控制台中键入变量值时,可以设置变量
摄氏度

celsius = float(input("Type in the celsius temperature: "))
函数
convert()
的作用是什么

def convert():

celsius=float(输入(“输入摄氏温度”)#如果你不介意的话,我会把它放在文章的编辑版本中!cf“Update”部分,因此display仅显示用户在convert函数中的输入。因此,我们应该创建新变量并调用我们希望值“传输”到的函数,因为没有更好的术语?您的意思是:
那么我们应该创建新变量并调用我们希望值“传输”到的函数,就像在convert()中一样,摄氏度和华氏度被定义为函数中的输入。因此,如果我们想在另一个函数中访问这些值,在将其传递给另一个函数之前,我们是否应该始终为一个值分配一个新变量?我很抱歉,如果我不明白,我不知道如何正确地表达我的东西。