0

I am wondering why the following program does not print 'b'. It is is very simple code; I think it must work; and do not know the reason why it doesn't.

def a():
    if b > 10:
        print 'b'
        sys.exit(1)

# main
while 1:
    a()
    b += 1

b is global variable. Actual code is more complicated but the structure is the same as mine. I guess when I call a() function and if b is greater than 10, it shows 'b'. However, it does not go inside if-statement.

Would you help me out how to solve?

Thanks.

9
  • Does it not print anything at all? Commented May 28, 2012 at 17:34
  • Wait, how does it print 'b' if it does not enter the body of the if? Please clarify. Commented May 28, 2012 at 17:37
  • I have tried your code with globla b in front of def a(): and it works fine with 'b' printed. It did go inside if-statement. (Python 2.7.2 Win32) Commented May 28, 2012 at 17:37
  • @Mayli Absolutely not! It raises NameError: global name 'b' is not defined. The solution is not the global b. It's b = 0 before the loop starts! Commented May 28, 2012 at 17:40
  • @Bird Jaguar IV: It does not print anything at all. Commented May 28, 2012 at 17:44

5 Answers 5

4

Globals are horrid learn not to use them, try something like this

import sys
def a(value):
    if value > 10:
        print value
        print "Greater than 10!"
        sys.exit(0)
b = 0
while True:
    a(b)
    b += 1
Sign up to request clarification or add additional context in comments.

5 Comments

I've tried to the same approach as you answered, but it does not work.
You need to delete the .pyc and restart the interpreter
While you are completely correct that globals should not be used, this doesn't actually answer the question.
@StevenBurnap The question is unclear and can't be reproduced
Other functions work very well, but this function cannot work correctly. I do not know why it is not working.
0

Global vars are not the usual way to go. You may prefer the usage of nested functions instead:

import sys

def outer(value=0):
    count = [value]
    print "started at:", count[0]
    def inner(x=1):
        print "current val:", count[0]
        count[0] += x
        if count[0] > 10:
            print "stopped at:", count[0]
            sys.exit(0)
    return inner

f = outer(5)
while True:
    f(1)

Comments

0

Another answers suggests not using globals, and I agree. If you still want to use globals, you should define b outside of the loop first.(if you do, then please post the complete code, because apart from that, it should work (and it does)).

Now, global b in the function definition is not necessary, because python guesses it is a global variable when you try to access it before assigning. But since it is not defined it raises an NameError:

NameError: global name 'b' is not defined

If you don't see that, so there's something else, you're not showing the actual code that has a problem.

This gives you in the end, something similar:

import sys
def a():
    global b
    if b > 10:
        print 'b'
        sys.exit(1)

b = 0
# main
while 1:
    a()
    b += 1

1 Comment

This works correctly. Is copy pasting it not working? I don't think so... Do you get any exception/output/anything... What is your actual code doing? We really need more details to help you, because that is perfectly fine.
0

You need to define b before "+="ing it.

def a():
    if b > 10:
        print 'b'
        sys.exit(1)

# main
b = 0 # HERE
while 1:
    a()
    b += 1

By the way, as many had told you: avoid globals.

Comments

0

just my 2 cents: forget global variables.

anyway, this should work

def a():
    global b
    if b > 10:
        print 'b'
        sys.exit(1)

EDIT FUUUUUUUUUUUUUUU

Although I fully agree with Jackob on how you should use functions arguments and avoid globals, just for the record, here's the solution with global:

def a():
    global b
    if b > 10:
        print 'b'
        sys.exit(1)

# main
b = 0
while 1:
    a()
    b += 1

1 Comment

Here too: -1 That global is redundant as b is not assigned to. And otherwise that code is identical to OP's.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.