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
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,524 Nov-07-2023, 09:49 AM
Last Post: buran
  It's saying my global variable is a local variable Radical 5 1,098 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  Delete all Excel named ranges (local and global scope) pfdjhfuys 2 1,689 Mar-24-2023, 01:32 PM
Last Post: pfdjhfuys
  Library scope mike_zah 2 796 Feb-23-2023, 12:20 AM
Last Post: mike_zah
  Scope of variable confusion Mark17 10 2,739 Feb-24-2022, 06:03 PM
Last Post: deanhystad
  Variable scope issue melvin13 2 1,507 Nov-29-2021, 08:26 PM
Last Post: melvin13
  Variable scope - "global x" didn't work... ptrivino 5 2,979 Dec-28-2020, 04:52 PM
Last Post: ptrivino
  Python Closures and Scope muzikman 2 1,775 Dec-14-2020, 11:21 PM
Last Post: muzikman
  Spyder Quirk? global variable does not increment when function called in console rrace001 1 2,157 Sep-18-2020, 02:50 PM
Last Post: deanhystad
  NameError for global variable leviporton 3 2,500 Jul-10-2020, 06:51 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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