Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a variable outside of function in Python

Tags:

python

A really simple question, and I'm sure I knew it but must have forgotten

When running this code:

x = 0
def run_5():
    print "5 minutes later"
    x += 5
    print x, "minutes since start"

run_5()
print x

I get x isn't defined. How can I have x used in the function and effected outside of it?

like image 361
Fergus Barker Avatar asked Dec 02 '22 04:12

Fergus Barker


1 Answers

Just return a value ?

x = 0
def run_5():
    print "5 minutes later"
    x += 5
    return x

x=run_5()
print x
like image 50
ghostdog74 Avatar answered Dec 05 '22 02:12

ghostdog74