0

I am newbee in Python . JUst wandering if it is possible to define a Global variable in Python.

I have this python code to compute total number of times Fib(2) has been accessed. But count is printing 0 when called.

import sys



def fib(n):
    """Takes input a natural number n > 0.
        Returns n! """

    global count
    count = 0
    if n == 1 or n == 0:
        return 1

    else:
        if n == 2:
            print 'encountered here::: '
            count += 1
        return fib(n-1) + fib(n-2)

def main():
    x = int(raw_input('Enter a natural number > 0 :'))
    print 'Fibonacci(%d) = %d' % (x, fib(x))
    print 'Count =', count
if 1:
    main()
7
  • 1
    Put global count inside the fib function. Commented Dec 2, 2013 at 12:09
  • I did. But when I want to print count from main after calling say fib(4) its printing 0. Commented Dec 2, 2013 at 12:14
  • Put count = 0 in module scope (after the import, for example). Be sure to keep the global count in fib. Commented Dec 2, 2013 at 12:20
  • 4
    As a beginner, you are probably better off assuming you cannot declare global variables, as this will help you avoid using them unnecessarily. Commented Dec 2, 2013 at 12:38
  • 1
    As chepner mentioned. Don't use global variables please. They can lead to unreadable code and are generally regarded as a bad habit. Commented Dec 2, 2013 at 13:29

4 Answers 4

3

A variable defined in the module outside of any function is considered a global variable. To modify a global variable inside of a function, you must write global variableName inside of the function.

You did this, however, that variable is only defined in your function, it must also be declared outside of the function to be considered a global variable. If not, it will be local to your function, despite the global in front of it.

TL;DR Also declare the variable outside of your function.

EDIT (I was wrong):

By writing global variableName in a function and executing said function, it does make the variable a global variable; if not you would be getting a NameError when you tried to print it.

The reason that you're getting a 0, however, is because everytime you call your function, you initialize count to 0. Nonetheless, the solution about still holds.

Sign up to request clarification or add additional context in comments.

Comments

2

Move count = 0 outside your fib(n) function. You only need to declare it as global count once inside the function, while the initialization should be outside. What you're doing is that you are re-initializing it with 0 every time the function is called.

import sys

count = 0

def fib(n):
    """Takes input a natural number n > 0.
        Returns n! """

    global count
    if n == 1 or n == 0:
        return 1

    else:
        if n == 2:
            print 'encountered here::: '
            count += 1
        return fib(n-1) + fib(n-2)

def main():
    x = int(raw_input('Enter a natural number > 0 :'))
    print 'Fibonacci(%d) = %d' % (x, fib(x))
    print 'Count =', count
if 1:
    main()

Although, you should avoid global variables.

Comments

0
global foo
foo = [1,2,3] 

Now foo can be used anywhere in the program

Comments

0

Put the count = 0 outside the function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.