Python Variable scopes
##################################################################### # YI_MoreOnScopes1.py # Mrs. Elia # 12/17/14 # Local and global variables ##################################################################### # # Definitions # ##################################################################### def intro(): message = ' Welcome to my world ' switch = 'off' print(switch) def closing(): switch = 'neither' print(switch) ## Main switch = 'on' print(switch) intro() closing() print(switch)
Classwork:
1. Change the definitions so each of them reassign the value of the global variable switch.
2. Remove the print statement in each of the definitions but print the value of the global variable switch after each definition call.
This is what it should look like:
# Main switch = 'on' print(switch) intro() ... print(switch) closing() ... print(switch)
Output:
on
off
neither
Homework:
The output should be: It is a wonderful day. Fix the problem without removing the definitions and without adding print staments to the definitions.
##################################################################### # YI_MoreOnScopes2.py # Mrs. Elia # 12/15/2014 # Local and global variables # ##################################################################### # Definitions ##################################################################### def intro(): word1 += "is a" word2 = "wonderful" return word2 def closing(): word4 = "day." ## Main word1 = "It" message = word1 message = intro() message = closing() print(message) # The output should be: It is a wonderful day. # Fix the problem without removing the definitions and # without adding print staments to the definitions.
The output should be:
It is a wonderful day.