-1

I'm trying to build if/elif/else statements based on an input statement and a function I defined. When I run the code, the input statement and the function definition and call run fine, but then I get to my if/elif/else statements and I get a NameError, telling me that a variable I call up in the if/elif/else statements is not defined (even though I thought I defined it in the function).

For context, I'm new to Python, and I'm building code in Atom and running it in Command Prompt.

My input, function definition, and if statement code looks like this:

h, m, s, t = input('Rate each of the following Emotions and hit enter:\nHappiness (1-7):'), input('Meaningfulness (1-7):'), input('Stressfulness (1-7):'), input('Tiredness (1-7):')
h_int=int(h)
m_int=int(m)
s_int=int(s)
t_int=int(t)
def na():
    add_h_m = h_int + m_int
    div_h_m = add_h_m/2
    add_s_t = s_int + t_int
    div_s_t = add_s_t/2
    net_affect = div_h_m - div_s_t
    print(net_affect)
na()

if net_affect >= 3:
    print("Doing Well")
elif net_affect <3 and net_affect >0:
    print("Doing Okay")
else:
    print("Not Good")

This is the Command Prompt output:

C:\Users\mrkev\Desktop\PAI 724>python ex2.py
Rate each of the following Emotions and hit enter:
Happiness (1-7):6
Meaningfulness (1-7):7
Stressfulness (1-7):4
Tiredness (1-7):3
3.0
Traceback (most recent call last):
  File "C:\Users\mrkev\Desktop\PAI 724\ex2.py", line 16, in <module>
    if net_affect >= 3:
NameError: name 'net_affect' is not defined

Where am I going wrong? I thought that by defining net_affect in the function, I could then use it as a variable in my program.

Thank you for your help!

  • Its best to posting a running example that demonstrates the problem. if you remove the `input` and just initialize `h_int` and etc..., plus put that `if net_affect >= 3:` in the same code block, we can copy/paste and get the same error you do. – tdelaney Oct 17 '21 at 20:05
  • @tdelaney okay good to know. I'll edit my question accordingly. Sorry, I'm new to StackOverflow and I'm still getting my bearings. – Mary Rachel Oct 17 '21 at 20:11
  • Does this answer your question? [Access a function variable outside the function without using "global"](https://stackoverflow.com/questions/19326004/access-a-function-variable-outside-the-function-without-using-global) – Tomerikoo Oct 18 '21 at 16:32

1 Answers1

2

Look for the 'variable scope' in your documentation or tutorials.

The net_affect variable is local to na function. You cannot access it from the outside, and it is deleted when the function returns.

You can alternatively :

  • define the variable outside of the function, and indicate that you are using it in the function

    net_affect = None
    def na():
        ...
        global net_affect
        net_affect = div_h_m - div_s_t
    
    
  • return the value from the function and catch the returned value

    def na():
        ...
        net_affect = div_h_m - div_s_t
        ...
        return net_affect                # return the value
    
    ...
    net_affect_new = na()                # catch the value
    

    Note: net_affect_new could be called net_affectbut they are not the same variables, they are in two different scopes.

hpchavaz
  • 1,368
  • 10
  • 16