Compare commits
23 Commits
733840f08c
...
7c04f33a1c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7c04f33a1c | ||
|
|
d122224f6e | ||
|
|
00fde385bb | ||
|
|
ddc79152be | ||
|
|
d0d8b12a7a | ||
|
|
e8923133fd | ||
|
|
8b70143137 | ||
|
|
2b58a02ab9 | ||
|
|
f843ac400e | ||
|
|
04509511c6 | ||
|
|
22c8c72221 | ||
|
|
039db3f4c8 | ||
|
|
ba710352b4 | ||
|
|
654349da4c | ||
|
|
5d635b6cbb | ||
|
|
d7bcea6443 | ||
|
|
cf41416449 | ||
|
|
a868adfe0e | ||
|
|
b60ae59dc8 | ||
|
|
cdc0e626dd | ||
|
|
1da26a13cd | ||
|
|
483474f6e5 | ||
|
|
20f929d6e4 |
10
Chapter_08/8-02_favorite_book.py
Normal file
10
Chapter_08/8-02_favorite_book.py
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
# Exercise 8-2 Favorite Book
|
||||||
|
# Learning Objective: Create a function that takes one argument.
|
||||||
|
|
||||||
|
title = input("What is a good book?")
|
||||||
|
|
||||||
|
|
||||||
|
def favorite_book(title):
|
||||||
|
print(f"One of my favorite books is {title}")
|
||||||
|
|
||||||
|
favorite_book(title)
|
||||||
7
Chapter_08/8-03_t-shirt.py
Normal file
7
Chapter_08/8-03_t-shirt.py
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
# Exercise 8-3 T-Shirt
|
||||||
|
# Learning Objective: Write a function with 2 arguments.
|
||||||
|
|
||||||
|
def make_shirt(size='S', message='your text here'):
|
||||||
|
print(f"\tNew shirt to be made:\n\tSIZE: {size}\n\tMESSAGE: {message}")
|
||||||
|
|
||||||
|
make_shirt("L", "Hello Python World")
|
||||||
8
Chapter_08/8-04_large_shirts.py
Normal file
8
Chapter_08/8-04_large_shirts.py
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
# Exercise 8-4 Large Shirts
|
||||||
|
# Learning Objective: Create function with default values.
|
||||||
|
|
||||||
|
def make_shirt(size='L', message='I love Python'):
|
||||||
|
print(f"\tNew shirt to be made:\n\tSIZE: {size}\n\tMESSAGE: {message}")
|
||||||
|
|
||||||
|
make_shirt("M", "Hello Python World")
|
||||||
|
make_shirt()
|
||||||
9
Chapter_08/8-05_cities.py
Normal file
9
Chapter_08/8-05_cities.py
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
# Exercise 8-5 Cities
|
||||||
|
# Learning Objective: Write a function that has a default value. Call it three times.
|
||||||
|
|
||||||
|
def describe_city(city, country='United States'):
|
||||||
|
print(f'{city.title()} is in {country.title()}')
|
||||||
|
|
||||||
|
describe_city('Chicago')
|
||||||
|
describe_city('Annapolis')
|
||||||
|
describe_city('Paris', 'France')
|
||||||
11
Chapter_08/8-06_city_names.py
Normal file
11
Chapter_08/8-06_city_names.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
# Exercise 8-6 City Names
|
||||||
|
# Learning Objective: Create function that returns a value
|
||||||
|
|
||||||
|
def city_country(city_name, country):
|
||||||
|
return f"{city_name.title()}, {country.title()}"
|
||||||
|
|
||||||
|
#Call 3 Times
|
||||||
|
|
||||||
|
print(city_country('Paris', 'France'))
|
||||||
|
print(city_country('Hamburg', 'Germany'))
|
||||||
|
print(city_country('Portland', 'United States'))
|
||||||
17
Chapter_08/8-07_album.py
Normal file
17
Chapter_08/8-07_album.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
# Exercise 8-7 Album
|
||||||
|
# Learning Objective: Write a function that returns a dictionary of information about an album.
|
||||||
|
|
||||||
|
def make_album(artist_name, album_title, num_tracks=None):
|
||||||
|
album_info = {'artist': artist_name, 'album': album_title}
|
||||||
|
if num_tracks:
|
||||||
|
album_info['number_of_tracks'] = num_tracks
|
||||||
|
return album_info
|
||||||
|
|
||||||
|
newAlbum = make_album('Korn', "Y'all wanna single", 1)
|
||||||
|
print(newAlbum)
|
||||||
|
|
||||||
|
newAlbum2 = make_album('Infected Mushroom', 'Classical Mushroom', 9)
|
||||||
|
print(newAlbum2)
|
||||||
|
|
||||||
|
newAlbum3 = make_album('Outsiders, Menog', 'Psychedelic Liquid')
|
||||||
|
print(newAlbum3)
|
||||||
32
Chapter_08/8-08_user_albums.py
Normal file
32
Chapter_08/8-08_user_albums.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
# Exercise 8-8 User Albums
|
||||||
|
# Learning Objective: Write a function with a while loop that gathers input and returns a dictionary of information about an album.
|
||||||
|
|
||||||
|
# VERY MESSY
|
||||||
|
|
||||||
|
def make_album(artist_name, album_title, num_tracks=None):
|
||||||
|
album_info = {'artist': artist_name, 'album': album_title}
|
||||||
|
if num_tracks:
|
||||||
|
album_info['number_of_tracks'] = num_tracks
|
||||||
|
return album_info
|
||||||
|
|
||||||
|
while True:
|
||||||
|
print("Welcome, entering 'q' will quit this program... \n")
|
||||||
|
gathered_artist = input("Please enter your artist: ")
|
||||||
|
if gathered_artist == 'q':
|
||||||
|
break
|
||||||
|
gathered_album = input("Now enter the album name: ")
|
||||||
|
if gathered_album == 'q':
|
||||||
|
break
|
||||||
|
desired_info = make_album(gathered_artist, gathered_album)
|
||||||
|
print(desired_info)
|
||||||
|
|
||||||
|
'''
|
||||||
|
newAlbum = make_album('Korn', "Y'all wanna single", 1)
|
||||||
|
print(newAlbum)
|
||||||
|
|
||||||
|
newAlbum2 = make_album('Infected Mushroom', 'Classical Mushroom', 9)
|
||||||
|
print(newAlbum2)
|
||||||
|
|
||||||
|
newAlbum3 = make_album('Outsiders, Menog', 'Psychedelic Liquid')
|
||||||
|
print(newAlbum3)
|
||||||
|
'''
|
||||||
10
Chapter_08/8-09_messages.py
Normal file
10
Chapter_08/8-09_messages.py
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
# Exercise 8-9 Messages
|
||||||
|
# Learning Objective: Pass a list to a function.
|
||||||
|
|
||||||
|
texts = ['hello', 'how are you?', 'i am well thanks']
|
||||||
|
|
||||||
|
def show_messages(messages):
|
||||||
|
for message in messages:
|
||||||
|
print(message)
|
||||||
|
|
||||||
|
show_messages(texts)
|
||||||
24
Chapter_08/8-10_sending_messages.py
Normal file
24
Chapter_08/8-10_sending_messages.py
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
# Exercise 8-10 Sending Messages
|
||||||
|
# Learning Objective: Manipulate a list from withim a function.
|
||||||
|
|
||||||
|
texts = ['message one', 'message two', 'message three']
|
||||||
|
sent_messages = []
|
||||||
|
|
||||||
|
def show_messages(messages):
|
||||||
|
for message in messages:
|
||||||
|
print(message)
|
||||||
|
|
||||||
|
def send_message(some_messages):
|
||||||
|
while some_messages:
|
||||||
|
to_be_sent = some_messages.pop()
|
||||||
|
sent_messages.append(to_be_sent)
|
||||||
|
print(f'{to_be_sent} has been sent')
|
||||||
|
|
||||||
|
|
||||||
|
#show_messages(texts)
|
||||||
|
|
||||||
|
send_message(texts)
|
||||||
|
print("SENT MESSAGES:\n")
|
||||||
|
print(sent_messages)
|
||||||
|
print('\nTEXTS:\n')
|
||||||
|
print(texts)
|
||||||
24
Chapter_08/8-11_archived_messages.py
Normal file
24
Chapter_08/8-11_archived_messages.py
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
# Exercise 8-11 Archived Messages
|
||||||
|
# Learning Objective: Prevent the modification but act upon a list within a function
|
||||||
|
|
||||||
|
texts = ['message one', 'message two', 'message three']
|
||||||
|
sent_messages = []
|
||||||
|
|
||||||
|
def show_messages(messages):
|
||||||
|
for message in messages:
|
||||||
|
print(message)
|
||||||
|
|
||||||
|
def send_message(some_messages):
|
||||||
|
while some_messages:
|
||||||
|
to_be_sent = some_messages.pop()
|
||||||
|
sent_messages.append(to_be_sent)
|
||||||
|
print(f'{to_be_sent} has been sent')
|
||||||
|
|
||||||
|
|
||||||
|
#show_messages(texts)
|
||||||
|
|
||||||
|
send_message(texts[:])
|
||||||
|
print("SENT MESSAGES:\n")
|
||||||
|
print(sent_messages)
|
||||||
|
print('\nTEXTS:\n')
|
||||||
|
print(texts)
|
||||||
20
Chapter_08/8-12_sandwiches.py
Normal file
20
Chapter_08/8-12_sandwiches.py
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
# Exercise 8-12 Sandwiches
|
||||||
|
# Learning Objective: Create a function that accepts arbitrary number of arguments.
|
||||||
|
|
||||||
|
order = []
|
||||||
|
|
||||||
|
ordering = True
|
||||||
|
print("Enter each ingredient one by one and hit enter. WHEN DONE type DONE to stop and process your order.")
|
||||||
|
while ordering == True:
|
||||||
|
sandwich_item = input("What can I add to your sandwich: \n")
|
||||||
|
if sandwich_item.lower() == 'done':
|
||||||
|
ordering = False
|
||||||
|
print("\tAdding {sandwich_item} to your sandwich...\n")
|
||||||
|
order.append(sandwich_item)
|
||||||
|
|
||||||
|
print(f"ORDER COMPLETE\nYou've ordered the following:\t")
|
||||||
|
for ingredient in order:
|
||||||
|
print(ingredient.lower())
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
16
Chapter_08/8-13_user_profile.py
Normal file
16
Chapter_08/8-13_user_profile.py
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
# Exercise 8-13 User Profile
|
||||||
|
# Learning Objective: Create a function that takes in two predefined key-value pairs and add 3 arbitrary ones.
|
||||||
|
|
||||||
|
|
||||||
|
# user_profile.py
|
||||||
|
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('mister', 'cheeks',
|
||||||
|
location='united states',
|
||||||
|
favorite_color='orange',
|
||||||
|
favorite_meal='scrambled eggs')
|
||||||
|
print(user_profile)
|
||||||
15
Chapter_08/8-14_cars.py
Normal file
15
Chapter_08/8-14_cars.py
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
# Exercise 8-14 Cars
|
||||||
|
# Learning Objective: Make a function that contains a dictionary and allows arbitrary numbers of arguments
|
||||||
|
# with two expected arguments of Manufacturer and Model Name
|
||||||
|
|
||||||
|
|
||||||
|
def make_car(make, model, **car_info):
|
||||||
|
car_info['Manufacturer'] = make
|
||||||
|
car_info['Model'] = model
|
||||||
|
return car_info
|
||||||
|
|
||||||
|
my_car = make_car('nissan', 'z', color='red', transmission='manual', rubber_floor_mats=True)
|
||||||
|
|
||||||
|
print(my_car)
|
||||||
|
|
||||||
|
|
||||||
@ -1,2 +1,3 @@
|
|||||||
## Left off:
|
## Left off:
|
||||||
### Chapter 8: Functions
|
### Chapter 8: Try It Yourself: 8-9 Messages
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user