The keyword global
Let's work more on your mastery of scope. In this exercise, you will use the keyword global within a function to alter the value of a variable defined in the global scope.
Bu egzersiz
Introduction to Functions in Python
kursunun bir parçasıdırEgzersiz talimatları
- Use the keyword
globalto alter the objectteamin the global scope. - Change the value of
teamin the global scope to the string"justice league". Assign the result toteam. - Hit the Submit button to see how executing your newly defined function
change_team()changes the value of the nameteam!
Uygulamalı interaktif egzersiz
Bu örnek kodu tamamlayarak bu egzersizi bitirin.
# Create a string: team
team = "teen titans"
# Define change_team()
def change_team():
"""Change the value of the global variable team."""
# Use team in global scope
# Change the value of team in global: team
# Print team
print(team)
# Call change_team()
change_team()
# Print team
print(team)