-1

I have a small problem in a bigger code... I can reproduce it in the following example

def graph(form): 
    if form == single:
        print 1
    if form == multi:
        print 2

When I type

graph(single)

I get

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-146-c730e3c6bbf1> in <module>()
----> 1 graph(single)

<ipython-input-143-cf1ff0a5e57e> in graph(form)
      5     if form == single:
      6         print 1
----> 7     if form == multi:
      8         print 2
      9 

NameError: global name 'multi' is not defined

1

Where is my mistake?

FrankTheTank
  • 1,579
  • 4
  • 14
  • 19
  • 2
    what are `single` and `multi` exactly? What do those variables refer to? – gravetii Apr 17 '14 at 12:50
  • are single and multi variables or constants? or are they meant to be text values in which case they should be enclosed in '' – gts Apr 17 '14 at 12:50
  • Just names.... With my real script I just want to decide if I want to plot one dataset or all datasets in one folder. – FrankTheTank Apr 17 '14 at 12:51
  • What do you mean by just names? If you want to refer them as string literals, you should enclose them in quotes. Like `graph("single")`. Likewise in the function definition. – gravetii Apr 17 '14 at 12:52
  • So, they should be strings then. – sashkello Apr 17 '14 at 12:52
  • @FrankTheTank looks like while removing irrelevant pieces of code (which is good) you have removed code that cause the problem as well (which is bad :)). Please provide that `if graph == ...` block as is. – J0HN Apr 17 '14 at 12:53
  • 'just names' does not make sense, are these names defined somewhere to equal to a value? e.g. for your example to work single must be a variable having a value assigned to it like single = (whatever value) and be visible in def graph's scope – gts Apr 17 '14 at 12:55
  • Well the code in my post is actually the part which causes problems... If I change single and multi to stings, it doesn't change anything – FrankTheTank Apr 17 '14 at 12:58
  • @FrankTheTank Please post the rest of your code, or at least wherever else you try and use things like `single`. – anon582847382 Apr 17 '14 at 12:59
  • The code in my post is the code which doesn't work. It doesn't need more code to show the problem. – FrankTheTank Apr 17 '14 at 13:01

2 Answers2

3

single and multi have no value. They are not defined anywhere in your function or globally.

You need to define what they mean before your function will work.

If they are just words, you need to wrap them in quotes "single" rather than single.

Vasili Syrakis
  • 9,321
  • 1
  • 39
  • 56
  • `single` apparently has a value, as the call succeeds as does the first `if` statement. – chepner Apr 17 '14 at 13:01
  • `def graph(form): if form == 'single': print 1 if form == 'multi': print 2` solved the problem. Now I can access the if statements with graph(multi) or graph(single) – FrankTheTank Apr 17 '14 at 13:05
1

The error is telling you where your problem resides:

NameError: global name 'multi' is not defined

Somewhere above, you either mistyped or forgot to define the multi variable. If these are globals defined outside your method, you should indicate that at the top of your method with the global statement. There's a good example of global usage here.

Community
  • 1
  • 1
Nathan
  • 2,093
  • 3
  • 20
  • 33