Hello.
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():
	get_celsius(celsius)
	farenheit=(9.0/5.0)*celsius+32
	print "It is",farenheit,"degrees outside."

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

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
    farenheit=(9.0/5.0)*new+32
    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
    main(celsius)

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()
    farenheit=(9.0/5.0)*celsius+32
    print "It is %s degrees outside.", farenheit

main(get_celsius)

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.