fork download
  1. #!/usr/bin/env python
  2. '''
  3. This script demonstrates scope.
  4. '''
  5.  
  6. # Let's create a variable and assign it an integer
  7. mynum = 3
  8.  
  9. print 'Here is my number:', mynum
  10.  
  11. def change_mynum(x):
  12. ## comment the next line
  13. global mynum
  14.  
  15. ## can also use: mynum += x
  16. mynum = mynum + x
  17.  
  18. ## return the new value
  19. return mynum
  20.  
  21. print 'change_mynum:', change_mynum(4)
  22.  
  23. print 'mynum:', mynum
Success #stdin #stdout 0.08s 8832KB
stdin
Standard input is empty
stdout
Here is my number: 3
change_mynum: 7
mynum: 7