# -*- coding: utf-8 -*- """ Created on Fri Oct 7 10:58:13 2022 @author: cpresser """ # Scope of variable: where it is available for use """ 1. Good variable naming avoids confusion. 2. Don't use gloabl variables that aren't constants in functions. """ # Global variable -- available anywhere after it is declared # x has global scope x = "Hello" def scopeFunction1(y): print(f"scopeFunction1: Global variable: {x}, argument: {y}") def scopeFunction2(y): #local variable x # this x is available inside the function x = 5 print(f"scopeFunction2: Local variable: {x}, argument: {y}") def scopeFunction3(y): #would like to change the global variable x global x x = 5 print(f"scopeFunction3: Global variable: {x}, argument: {y}") #get global information into a function def scopeFunction4(y, z): # can change z, wihtout chaning the global x z = 1 print(f"scopeFunction4: argument1: {y}, argument2: {z}") print(f"Global variable {x}") scopeFunction1(7) scopeFunction2(7) print(f"Global variable {x}") scopeFunction3(12) print(f"Global variable {x}") x = "I'm back!" scopeFunction1(7) print(f"Global variable {x}") scopeFunction4(7, x) print(f"Global variable {x}") # print(y) y is only available within the functions y = True #global variable y print(True)