I'm currently doing some review for my exam, and I have a strange problem or I might be overseeing an extremely simple mistake that I for some reason can't catch. I'm practicing with functions and parameter passing between these functions, and I whipped up a really simple program to make sure I understood the basics:
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)
Now I'm not sure if the return values are needed, but I put them there because I remember my professor saying it's important.
Now, the issue is when I try to run this, it tells me that the variables that should be passing are not even recognized when the program runs. Can someone explain why this is the issue?
And for future reference, if I wanted to pass these variables between 2 or more functions, how will I do that?
UPDATE: ADDING FOLLOWING TEXT FOR FURTHER CLARIFICATION
Here's some code my professor provided me with as an exemplar. How does he pass those local variables?
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()
You have a problem understanding how functions work ...
The scope of the variables defined within a function does not exceed the function itself. That is to say, if you get out of the function, these variables (if they are not global) do not exist anymore.
You should either define the variables before calling the function, or pass them as keyword arguments to give them values by default:
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)
and call it like this :
temperature(celsius, fahrenheit)
Or using keywords :
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)
Calling it like this :
temperature() # --> use the default parameters
#or
temperature(celsius=0, fahrenheit=10) --> change the values of your parameters
But in both ways, there is no need to overwrite the variables within the function as you've done.
Update :
The value of the variable celsius
is set by the user when typing the value of the variable in the console :
celsius = float(input("Type in the celsius temperature: "))
What does the function convert()
do ? :
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
takes the value entered by the user as a string
, then he casts it to an int
.