Answers for "global vs local scope python"

4

global vs local variables

# in functions we use Global vs Local variables
# Global means as the name suggests it's Global (accessed anywhere in file)

def player_health():
  # Local variable 
  # Local variables can't be accessed outside of a function
  player_health = 78
  
# error
print(player_health)


# Global Variables
player_strength = 2
def increase_strength():
	player_strength = 3		# this will give you an error because the player_strength
    # is defined in the global scope so you can't modify it in a function
    
    # if you try to print the global variable in function you can do it
    print(player_strength)
    
# Local
	# only accessed/edited by the functions only
# Global
	# accessed/edited in global level not in functions
    # only accessed by functions but not editable
    
# if you got something by this example please upvote it
Posted by: Guest on January-07-2021
2

global and local scope in python

########Scopes#######
#local scope 
def drink_portion():
    portion_strenth = 10
    return portion_strenth
print(drink_portion())
#print(portion_strength) #will give error

###########global scope###########

player_Health = 100
def Got_hit():
    health_dropped = 5
    leval = player_Health - health_dropped
    return leval
print(Got_hit())
Posted by: Guest on August-29-2021

Code answers related to "global vs local scope python"

Python Answers by Framework

Browse Popular Code Answers by Language