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


Messages In This Thread
Help with Global/Coerced Variable (Understanding Scope) - by Rev2k - Jan-09-2020, 03:12 AM

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,899 Nov-07-2023, 09:49 AM
Last Post: buran
  It's saying my global variable is a local variable Radical 5 1,224 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  Delete all Excel named ranges (local and global scope) pfdjhfuys 2 1,840 Mar-24-2023, 01:32 PM
Last Post: pfdjhfuys
  Library scope mike_zah 2 878 Feb-23-2023, 12:20 AM
Last Post: mike_zah
  Scope of variable confusion Mark17 10 2,926 Feb-24-2022, 06:03 PM
Last Post: deanhystad
  Variable scope issue melvin13 2 1,590 Nov-29-2021, 08:26 PM
Last Post: melvin13
  Variable scope - "global x" didn't work... ptrivino 5 3,092 Dec-28-2020, 04:52 PM
Last Post: ptrivino
  Python Closures and Scope muzikman 2 1,853 Dec-14-2020, 11:21 PM
Last Post: muzikman
  Spyder Quirk? global variable does not increment when function called in console rrace001 1 2,259 Sep-18-2020, 02:50 PM
Last Post: deanhystad
  NameError for global variable leviporton 3 2,585 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