-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathe_scope_3.py
38 lines (33 loc) · 1017 Bytes
/
e_scope_3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# example 3 of badly using scope of variables
# this can be very confusing (and cause trouble) because we use
# the variable names food and drink both inside and outside of functions
# we actually have 2 DIFFERENT variables with the name food and
# 2 DIFFERENT variables with the name drink
# global variables are outside all functions
food = "pizza"
drink = "beer"
# local variable food
def food(meal):
if meal == "breakfast":
food = 'eggs'
elif meal == "lunch":
food = 'salad'
else:
food = None
return food
# local variable drink
def drink(meal):
if meal == "breakfast":
drink = 'coffee'
elif meal == "lunch":
drink = 'iced tea'
else:
drink = None
return drink
# run the functions and store what is returned, changing
# the value of both global variables
food = food("lunch")
drink = drink("breakfast")
# print global variables - note, now they have changed
print("The current food: " + food)
print("The current drink: " + drink)