xxxxxxxxxx
variable = "Hello"
def function():
global variable
print(variable)
function()
xxxxxxxxxx
# Solution 1: Mark the Variable globally
Student = 'Anna'
def names():
global Student
print(Student)
Student = 'Lily'
names()
names()
# Solution 2: Using Function Parameter Value
def names(students):
print(students)
students = 'Lily'
print(students)
names('Alex')
# Solution 3: Using nonlocal Keyword
def students():
name = 'Lily'
def marks():
nonlocal name
print(name)
name = 'Alex'
print(name)
marks()
students()