Python Forum
Help with Global/Coerced Variable (Understanding Scope)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with Global/Coerced Variable (Understanding Scope)
#1
Hi, I'm going through the python in easy steps book and have hit a snag with global/coerced variables.
I've copied the code exactly from the book but not getting the return I expect.

Here is the code:

global_var = 1
def my_vars():
    print( "Global Variable:" , global_var )

    local_var = 2
    print( "Local Variable:" , local_var )

    global inner_var
    inner_var = 3

    my_vars()
    print( "Coerced Global:" , inner_var )
This is what I get back when I run it:

Error:
C:\Users\rev\PycharmProjects\untitled1\venv\Scripts\python.exe C:/MyScripts/scope.py Process finished with exit code 0
What I should be getting back is:
Global Variable: 1
Local Variable: 2
Coerced Variable 3

Any ideas as I've copied this exactly from the book.
Thanks in advance Smile
Reply
#2
You need to call the function. Add this line to the end:

my_vars()
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thx for the reply ich - I'm now getting this back(not even the full return as it goes on forver and is too large to post):

Error:
Global Variable: 1 Local Variable: 2 Global Variable: 1 Local Variable: 2 Traceback (most recent call last): File "C:/MyScripts/scope.py", line 14, in <module> my_vars() File "C:/MyScripts/scope.py", line 11, in my_vars my_vars() File "C:/MyScripts/scope.py", line 11, in my_vars my_vars() File "C:/MyScripts/scope.py", line 11, in my_vars my_vars() [Previous line repeated 993 more times] File "C:/MyScripts/scope.py", line 3, in my_vars print( "Global Variable:" , global_var ) RecursionError: maximum recursion depth exceeded while calling a Python object Global Variable: 1 Local Variable: 2 Global Variable: 1 Local Variable: 2 Global Variable: 1 Local Variable: 2 Global Variable: 1 Local Variable: 2 Global Variable: 1 Local Variable: 2 Global Variable: 1 Local Variable: 2 Process finished with exit code 1
Reply
#4
Show your modified code. The line I asked you to add should not be indented.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
modified code:

global_var = 1
def my_vars():
    print( "Global Variable:" , global_var )

    local_var = 2
    print( "Local Variable:" , local_var )

    global inner_var
    inner_var = 3

    my_vars()
    print( "Coerced Global:" , inner_var )
my_vars()
Reply
#6
I think you shouldnt be calling my_vars() inside the definition, that is which causing the loop.

global_var = 1
def my_vars():
    print( "Global Variable:" , global_var )
 
    local_var = 2
    print( "Local Variable:" , local_var )
 
    global inner_var
    inner_var = 3
 
    #my_vars()  
    print( "Coerced Global:" , inner_var )
my_vars()
Output:
python test4.py Global Variable: 1 Local Variable: 2 Coerced Global: 3
Best Reegards,
Sandeep

GANGA SANDEEP KUMAR
Reply
#7
Thank you so much for solving my issue, both of you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable scope - "global x" didn't work... ptrivino 5 613 Dec-28-2020, 04:52 PM
Last Post: ptrivino
  Python Closures and Scope muzikman 2 470 Dec-14-2020, 11:21 PM
Last Post: muzikman
  Spyder Quirk? global variable does not increment when function called in console rrace001 1 436 Sep-18-2020, 02:50 PM
Last Post: deanhystad
  NameError for global variable leviporton 3 761 Jul-10-2020, 06:51 PM
Last Post: bowlofred
  Block of code, scope of variables and surprising exception arbiel 8 1,171 Apr-06-2020, 07:57 PM
Last Post: arbiel
  Function Recognises Variable Without Arguments Or Global Variable Calling. OJGeorge4 1 712 Apr-06-2020, 09:14 AM
Last Post: bowlofred
  Global Variable Not Updating joew 2 3,562 Jan-26-2020, 04:15 PM
Last Post: joew
  Solving a scope issue profconn1 4 992 Nov-01-2019, 07:46 PM
Last Post: profconn1
  Declaring a Global Variable in a Function Bob1948 4 1,119 Sep-14-2019, 11:16 PM
Last Post: ichabod801
  Global variable does not seem to be global. Columbo 6 1,477 Jul-15-2019, 11:00 PM
Last Post: Columbo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020