I am a n00b learning to code in Python. Right now, I am working on functions and parameters. To practice, I am making a simple Celsius-to-Farenheit converter. However,whenever I run the program it returns a NameError. I am confused in my n00bishness.
Please reply.

Here is my code:

>>> def get_celsius(celsius):
	celsius=input("Type in the Celsius temperature:" )
	return celsius

>>> def main():
	print "It is",farenheit,"degrees outside."

Here is my error:
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
File "<pyshell#10>", line 2, in main
NameError: global name 'celsius' is not defined

6 Years
Discussion Span
Last Post by Joeflims

That is because the variable 'celsius' was never defined.

def get_celsius(celsius):
    celsius=int(raw_input("Type in the Celsius temperature: " )) # use 'int(raw_input())' instead of 'input()'
    return celsius
def main(celsius): # Pass the variable in
    new = get_celsius(celsius) # When you return a value, you should store it in a variable
    print "It is",farenheit,"degrees outside."
if __name__ == '__main__': # Run if not imported
    # Get the temperature in Celsius 1st
    celsius = int(raw_input('Enter the temperature in Celsius: '))
    # Start the program

When I try to run the code, it says that celsius is not defined.


redyugi now you have 2 "int(raw_input" lines that do the same.

To simplify it.
The first function we dont give an argument,just retun value out.
Remember run this code as a script and not in python shell(IDLE)

def get_celsius():
    celsius = int(raw_input("Type in the Celsius temperature:" ))
    return celsius

def main(get_celsius):
    celsius = get_celsius()
    print "It is %s degrees outside.", farenheit


Edited by snippsat: n/a


i think you need some explanation on functions and parameters.

Let me try to provide you one.

Ever studied the quadratic function in HS? Remember how the equation is expressed as:

f(x) = x^2 + 2x - 4

The f, is equivalent to a function in programming, this function executes the equation, x^2 + 2x - 4. The x is the parameter, if we were to evaluate f(6), it would be (6)^2 + 2(6) - 4

Translating that to python it would be

def f(x):
    return (x**2) + (2*x) - 4

now if you were to call the function by saying f(x) like you were doing, it's not going to do anything, because, x is just an arbitrary variable that has no value attached to it.

if you were to assign d (this is another variable) to be 5 via d = 5 before you call the function via f(d), it would become f(5)

d = 5
print f(d) # this is actually going to become f(5)

or simply:

print f(5)

once we call a function, we enter the code of the function. parameters are just a variable that's set INSIDE the function (more on that , google search global and local variables or read that in the think python book i'll introduce you at the end of the psot). They are NOT accessible outside the function. Back to the f(5), what happen is that x inside the function f will hold the value 5. and the code will be evaluated as (5**2) + (2*5) - 4, which would become 31.

Another more complicated example, where we have multiple parameters

def printmultipletimes(msg, num_of_time):
    for i in range(num_of_time+1):
        print msg

this function takes 2 parameters. the first parameter is the msg to be printed, the second is the number of times it will print it.

if you were to call the function by printmultipletimes("hello world!", 5), the msg and num_of_times inside the function printmultipletimes will become "hello world" and 5 respectively. This will then proceed to loop 5 times and print the message with each loop.

Is it clearer now? I suggest you to read through the book think python as it serve as an excellent book for anyone who wants to start programming in general. I assume you didn't transfer from a different programming language as you don't know the basics yet.

Edited by ultimatebuster: n/a


@ultimatebuster You are correct. This is my first programming language. Thank you so much.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.