需要通过函数调用进行一些清理
Need some clearing up with function calls
我目前正在复习我的考试,我遇到了一个奇怪的问题,或者我可能正在监督一个非常简单的错误,但由于某种原因我无法发现。我正在练习函数和这些函数之间的参数传递,我编写了一个非常简单的程序以确保我了解基础知识:
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)
现在我不确定是否需要 return 值,但我把它们放在那里是因为我记得我的教授说这很重要。
现在,问题是当我尝试 运行 时,它告诉我应该传递的变量在程序 运行 时甚至无法识别。有人可以解释为什么这是问题吗?
为了将来参考,如果我想在 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()
在调用 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)
然后就可以了
您收到错误,因为您声明的变量范围 '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)
或使用关键字:
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
。
我目前正在复习我的考试,我遇到了一个奇怪的问题,或者我可能正在监督一个非常简单的错误,但由于某种原因我无法发现。我正在练习函数和这些函数之间的参数传递,我编写了一个非常简单的程序以确保我了解基础知识:
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)
现在我不确定是否需要 return 值,但我把它们放在那里是因为我记得我的教授说这很重要。
现在,问题是当我尝试 运行 时,它告诉我应该传递的变量在程序 运行 时甚至无法识别。有人可以解释为什么这是问题吗?
为了将来参考,如果我想在 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()
在调用 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)
然后就可以了
您收到错误,因为您声明的变量范围 '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)
或使用关键字:
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
。