-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExercise8.py
More file actions
236 lines (192 loc) · 6.71 KB
/
Exercise8.py
File metadata and controls
236 lines (192 loc) · 6.71 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#8.1 Write a function
"""
def display_message():
print("I learned how to make a function with any parameters")
print("I learned how to make a function without parameters")
display_message()
"""
#8.2 write a function that acceptos one parameter
"""
def favorite_book(book):
print(f"{book} is one of my favorite books")
favorite_book("Python Crash Course")
"""
#8.3 make a function call it and call it again with keywords arguments
"""
def make_shirt (size, message):
print("Im going to make a", size, "t-shirt")
print('it will say, "', message, '"')
make_shirt("large", "I love python")
make_shirt(size = "medium", message="I love python")
"""
#8.4 modify a function 8.3 with arguments as default
"""
def make_shirt (size= "large", message= "I love Python"):
print("Im going to make a", size, "t-shirt")
print('it will say, "', message, '"')
make_shirt()
make_shirt(size = "medium")
make_shirt("medium", "Programmers are loppy")"""
#8.5 write a function that accepts 2 arguments, parameter city as defautl
"""
def describe_city(city, country = "Colombia"):
print(f"{city} is in {country}")
describe_city("Bogota")
describe_city("MDX", "Mexico")
describe_city("valledupar")
"""
#8.6 Write a function that takes in the name of a city and its country and return full string
"""
def city_country(city,country):
return(city.title()+ " "+ country.title())
city = city_country("santigado", "Chile")
print(city)
city = city_country("Buenos aires", "Argentina")
print(city)
"""
#8.7 Wirte a functiono and buiild a dictionary that contains artis-album and return it
"""
def make_album(artist, album, tracks = ""):
albums = {
"artist" : artist.title(),
"album" : album.title(),
}
if tracks:
albums["tracks"] = tracks.title()
return albums
album = make_album("Metalica", "black album")
print(album)
album = make_album("Incubus", "Greatest hits")
print(album)
album = make_album("Metalica", "black album", "15")
print(album)
"""
#8.8 With Exercise 8.7 add a while loop for to permit do input for teh user
"""
def make_album(artist, album, tracks = ""):
albums = {
"artist" : artist.title(),
"album" : album.title(),
}
if tracks:
albums["tracks"] = tracks.title()
return albums
while True:
print("Enter '<q>' at any time to stop")
title =input("\nWhat album are you thinking of ?")
if title == 'quit':
break
artist = input("who's the artist ?")
if artist == 'quit':
break
album = make_album(artist, title)
print(album)
print("\nThanks for responding!")
"""
#8.9 Make a list, pas the list to a function, print each name in the list
"""
magicians_names = ["daniel", "sergio", "jeison"]
def show_magicians():
for name in magicians_names:
print("The name of magician is ",name.title())
show_magicians()
"""
#8.10 copu from list 8.9 , make a function that modifies the list adding teh phrase"the great"
"""
magicians_names = ["daniel", "sergio", "jeison"]
def show_magicians(magicians_names):
#Print the name of each magician in the list
for name in magicians_names:
print(name.title())
def make_great(magicians_names):
#Add " the Great!" to each magician's name.
#build a new list to hold the great magicians
great_magicians = []
#Make each magician great, and add it to great_magicians.
while magicians_names:
magician = magicians_names.pop()
great_magician = magician + " the Great"
great_magicians.append(great_magician)
#Add the great magicians back into magicians.
for great_magician in great_magicians:
magicians_names.append(great_magician)
show_magicians(magicians_names)
print("\n")
make_great(magicians_names)
show_magicians(magicians_names)
"""
#8.1 with exercise 8.10, call the function make_great with a copy of the list of magicians_names
#return the new list and store in a separate list
"""
magicians_names = ["daniel", "sergio", "jeison"]
def show_magicians(magicians_names):
#Print the name of each magician in the list
for name in magicians_names:
print(name.title())
def make_great(magicians_names):
#Add " the Great!" to each magician's name.
#build a new list to hold the great magicians
great_magicians = []
#Make each magician great, and add it to great_magicians.
while magicians_names:
magician = magicians_names.pop()
great_magician = magician + " the Great"
great_magicians.append(great_magician)
#Add the great magicians back into magicians.
for great_magician in great_magicians:
magicians_names.append(great_magician)
return magicians_names
show_magicians(magicians_names)
print("\nlist Great magicians :")
great_magicians = make_great(magicians_names[:])
show_magicians(great_magicians)
print("\nOriginal list magicians")
show_magicians(magicians_names)
"""
#8.12 Write a function that accepts several parameters, it should have one paremeter that collects n arguments
"""
def make_sandwich (*items):
#Make a sandwich with the given items
print("\nI'll make you a great sandiwch: ")
for item in items:
print(f" ... adding {item} to your sandwich" )
print("your sandwich is ready")
make_sandwich("roast beef", "cheddar chesse", "lettuce")
make_sandwich("roast beef", "peanut butter","honey mustard", "lettuce")
"""
#8.13 start with a copy user_profile.py page 153. build the function with your information
"""
def build_profile(first, last, **user_info):
#Build a dictionary containing everything we know about a user.
profile = {}
profile['first_name'] = first
profile['last_name'] = last
for key, value in user_info.items():
profile[key] = value
return profile
user_profile = build_profile('Jhon', 'Erick',
location=' United Kingdom',
height='1.69 cm', weight = "71.6 kg")
print(user_profile)
"""
#8.14 Write a function store information about the car < manufacturer> <model>
# and accept an arbitrary number of keyword arguments.store in dictionary
"""
def make_car(manufacturer, model, **feautres):
car= {
"manufacturer": manufacturer,
"model " : model
}
for feature,value in feautres.items():
car[feature] = value
return car
car = make_car("subaru", "outback", color = "blue", tow_package = True)
print(car)
"""
#8.15 import functions from modules
#module name is " modulo "
#import modulo #importa todo el modulo
#from modulo import function_name # importa x funcion del modulo
#from modulo import function_name as fn #importa x funcion del modulo con un alias
#import modulo as md # importa el modulo con un alias
#from modulo import* # importa el modulo sin necesidad de usar alias en las funciones