-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_exercise.py
217 lines (146 loc) · 5.05 KB
/
function_exercise.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#message
def display_message():
"""Display a simple message"""
print("I am learning about functions in python")
display_message()
#favorite book
def favorite_book(title):
"""Display a message about someone's favorite book"""
print(f"My favorite book is {title.title()}")
favorite_book('alice in wonderland')
#t-shirt
def make_shirt(size, message):
"""Display a message about the size of the shirt and the message printed on it"""
print(f"The size of the shirt is {size} and the message printed on it is {message}")
make_shirt('large', 'hello world') #positional arguments
make_shirt(size='large', message='hello world') #keyword arguments
#large shirts
def make_shirt(size='large', message='I love Python'):
"""Display a message about the size of the shirt and the message printed on it"""
print(f"The size of the shirt is {size} and the message printed on it is {message}")
make_shirt()
#cities
def describe_city(city, country='Kenya'):
"""Display a message about a city and the country it is in"""
print(f"{city.title()} is in {country.title()}")
describe_city('nairobi')
describe_city('mombasa')
describe_city('kampala', 'uganda')
#city name
def city_country(city, country):
"""Return a string like 'Santiago, Chile'"""
return f"{city.title()}, {country.title()}"
city = city_country('nairobi', 'kenya')
print(city)
#three city-country pairs
def city_country(city, country):
"""Return a string like 'Santiago, Chile'"""
return f"{city.title()}, {country.title()}"
city1 = city_country('nairobi', 'kenya')
city2 = city_country('mombasa', 'kenya')
city3 = city_country('kampala', 'uganda')
print(city1)
print(city2)
print(city3)
#album
def make_album(artist, title, tracks='None'):
"""Return a dictionary of information about an album"""
album = {'artist': artist, 'title': title}
if tracks:
album['tracks'] = tracks
return album
album1 = make_album('khaligraph', 'testimony')
album2 = make_album('sauti sol', 'midnight train')
album3 = make_album('beyonce', 'lemonade')
album4 = make_album('beyonce', 'lemonade', tracks=12)
print(album1)
print(album2)
print(album3)
print(album4)
#user albums
def make_album(artist, title, tracks='None'):
"""Return a dictionary of information about an album"""
album = {'artist': artist, 'title': title}
if tracks:
album['tracks'] = tracks
return album
while True:
print("\nPlease enter the artist and album name:")
print("(enter 'q' at any time to quit)")
artist = input("Artist: ")
if artist == 'q':
break
title = input("Title: ")
if title == 'q':
break
album = make_album(artist, title)
print(album)
#messages
text_messages = ['hello', 'how are you?', 'what are you doing?']
def show_messages(messages):
"""Print all messages in a list"""
for message in messages:
print(message)
show_messages(text_messages)
#sending messages
text_messages = ['hello', 'how are you?', 'what are you doing?']
sent_messages = []
def send_messages(messages, sent_messages):
"""Print each message and move it to sent_messages"""
while messages:
current_message = messages.pop()
print(f"Sending message: {current_message}")
sent_messages.append(current_message)
send_messages(text_messages, sent_messages)
print("\nFinal lists:")
print(text_messages)
print(sent_messages)
#archived messages
text_messages = ['hello', 'how are you?', 'what are you doing?']
sent_messages = []
def send_messages(messages, sent_messages):
"""Print each message and move it to sent_messages"""
while messages:
current_message = messages.pop()
print(f"Sending message: {current_message}")
sent_messages.append(current_message)
def show_messages(messages):
"""Print all messages in a list"""
for message in messages:
print(message)
send_messages(text_messages[:], sent_messages)
print("\nFinal lists:")
print(text_messages)
print(sent_messages)
#sandwiches
def make_sandwich(*items):
"""Make a sandwich with the given items"""
print(items)
make_sandwich('cheese')
make_sandwich('cheese', 'ham')
make_sandwich('cheese', 'ham', 'lettuce')
def make_sandwich(*items):
"""Make a sandwich with the given items"""
print("\nMaking a sandwich with the following items:")
for item in items:
print(f"- {item}")
make_sandwich('cheese')
make_sandwich('cheese', 'ham')
make_sandwich('cheese', 'ham', 'lettuce')
#user profile
def build_profile(first, last, **user_info):
"""Build a dictionary containing everything we know about a user"""
user_info['first_name'] = first
user_info['last_name'] = last
return user_info
user_profile = build_profile('albert', 'einstein', location='princeton', field='physics')
print(user_profile)
#cars
def car_info(make, model, **car_details):
"""Build a dictionary containing everything we know about a car"""
car_details['make'] = make
car_details['model'] = model
return car_details
car = car_info('subaru', 'outback', color='blue', tow_package=True)
print(car)
#printing models