Skip to content

Instantly share code, notes, and snippets.

@jmangrad
Last active October 18, 2022 17:42
Show Gist options
  • Save jmangrad/714182554fdf10af98bf46aaf9e0b412 to your computer and use it in GitHub Desktop.
Save jmangrad/714182554fdf10af98bf46aaf9e0b412 to your computer and use it in GitHub Desktop.
Python Crash Course
8-6. City Names: Write a function called city_country() that takes in the name
of a city and its country. The function should return a string formatted like this:
"Santiago, Chile"
Call your function with at least three city-country pairs, and print the
values that are returned.
8-7. Album: Write a function called make_album() that builds a dictionary
describing a music album. The function should take in an artist name and an
album title, and it should return a dictionary containing these two pieces of
information. Use the function to make three dictionaries representing different
albums. Print each return value to show that the dictionaries are storing the
album information correctly.
Use None to add an optional parameter to make_album() that allows you to
store the number of songs on an album. If the calling line includes a value for
the number of songs, add that value to the album’s dictionary. Make at least
one new function call that includes the number of songs on an album.
8-8. User Albums: Start with your program from Exercise 8-7. Write a while
loop that allows users to enter an album’s artist and title. Once you have that
information, call make_album() with the user’s input and print the dictionary
that’s created. Be sure to include a quit value in the while loop.
8.6 Answer
def city_country(city, country):
city_location = "{}, {}".format(city.title(), country.title())
print(city_location)
return city_location
city_country('london', 'United Kingdom')
city_country('paris', 'france')
city_country('sydney', 'australia')
8.7 answer
def make_album(artist_name, album_title, number_of_songs=None):
album = {'artist name': artist_name, 'album_title': album_title}
if number_of_songs:
album['number of songs'] = number_of_songs
return album
musician = make_album('Kanye west', 'Ye')
print(musician)
musician = make_album('Kanye west', 'Ye', number_of_songs=13)
print(musician)
8.8 answer
def make_album(artist_name, album_title, number_of_songs=None):
album = {'artist name': artist_name, 'album_title': album_title}
if number_of_songs:
album['number of songs'] = number_of_songs
return album
while True:
singer = input("Who is your favorite singer? ").title()
if singer == 'q' or 'Q':
break
song = input("What is your favorite song of that singer? ").title()
if song == 'q' or 'Q':
break
musician = make_album(singer, song)
print(musician)
@Whoste
Copy link

Whoste commented Oct 18, 2022

How cool that I stumbled upon such a collection of examples and assignments and I honestly did not even look at the answer and tried to do everything myself, checking it against the result. I like to experience the feeling of difficulties and their preliminaries, even back in college I did essays on this topic guided by examples from https://eduzaurus.com/free-essay-samples/challenges/ to understand the vector in which you can write. Now I decided to study Python Crash for myself, and this book is a great way to study it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment