Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Problem with Python function and variables

I am having trouble with variables in a function. The function works properly. However, I cannot get access to the variables outside of the function. I simplified the script so I could focus on the problem but cannot figure it out. Must be something simple. Here is the script followed by NameError message.

def finish_up(temp, mode):

if temp < 60: mode = "Heat" else: mode = "Cool" print(mode) print(temp) return temp, mode finish_up(99,"Neutral")

print(mode) print(temp) print("The function has completed its job")

treehouse:~/workspace$ python indigo.py Cool 99 Traceback (most recent call last): File "indigo.py", line 15, in <module> print(mode) NameError: name 'mode' is not defined

boi
boi
14,242 Points

It will be much easier to solve your problem if you post your full code with proper format or "fork workspace" and give the link here.

3 Answers

boi
boi
14,242 Points

This is your original code πŸ‘‡

# Set thermostats to Heat if dining room temperature is less than 60 else set them to Cool

def finish_up(temp, mode):

    if temp < 60:
        mode = "Heat"
    else:
        mode = "Cool"
    print(mode)
    print(temp)
    return temp, mode

finish_up(99,"Neutral")
temp, mode = finish_up() πŸ‘ˆ#Look here, All you have to do is input 99,"Neutral" in the function, and it would work fine
print(mode)
print(temp)
print("The function has completed its job")
#

Correct me If I'm wrong, you want the value of the variables to be in use outside of the function? If Yes, in that case, It seems your original code already solves your problem. Few examples to get things clearπŸ‘‡

# Set thermostats to Heat if dining room temperature is less than 60 else set them to Cool

def finish_up(temp, mode):

    if temp < 60:
        mode = "Heat"
    else:
        mode = "Cool"
    print(mode)
    print(temp)
    return temp, mode

finish_up(99,"Neutral")
temp, mode = finish_up(99,"Neutral")πŸ‘ˆ #Made changes here
print(mode)
print(temp)
print("The function has completed its job")
#


#Another example πŸ‘‡
# Set thermostats to Heat if dining room temperature is less than 60 else set them to Cool

def finish_up(temp, mode):

    if temp < 60:
        mode = "Heat"
    else:
        mode = "Cool"
    #print(mode)πŸ‘ˆ
    #print(temp)πŸ‘ˆ
    return temp, mode                                #Made a few changes for cleaner code

#finish_up(99,"Neutral")πŸ‘ˆ
temp, mode = finish_up(99,"Neutral")
print(mode)
print(temp)
print("The function has completed its job")
#

Did this help you?

Btw I learned something new by reading your code, thank you πŸ‘

And If you want to learn how to properly format your code like In my examples, then just watch this video

Post here again if there is some problem

Sorry the formatting was not correct. I don't see a way to show code accurately. You can tell I am new at this. I took a snapshot and is at https://w.trhou.se/a3j4nlp4ff The script I am having trouble with is indigo.py. I am having trouble with variables in a function. The function works properly. However, I cannot get access to the variables outside of the function. The error message was:

treehouse:~/workspace$ python indigo.py Cool 99 Traceback (most recent call last): File "indigo.py", line 15, in <module> print(mode) NameError: name 'mode' is not defined

Yes, your response was very helpful. Making variables = a function is not intuitive to me as in temp, mode = finish_up(49,"Neutral"). Wouldn't another solution be to use global variables so that temp and mode can be used inside of functions or in the main script? Also, thanks for the video explaining ticks very helpful.

John

-- Dr. John R. Patrick Mobile: +1 203 526-3680 Web: johnpatrick.com Wikipedia: en.wikipedia.org/wiki/John_R._Patrick FB: facebook.com/john.patrick.3110 Tw: twitter.com/johnrpatrick LinkedIn: linkedin.com/in/jrpatrick

My books on Amazon: See the johnpatrick.com

boi
boi
14,242 Points

I have not learned (properly) about global variables yet (But have a good idea of it), I tried to explain with a basic approach, so I used a very basic explanation, however global variables could be used but you have to watch out for some "Gotcha" (Problems that could arise).

Good luck with your book Dr.John πŸ‘