def display_message():
print(f"Hey! I am learning chapter 8")
display_message()
Hey! I am learning chapter 8
Write a function called display_message() that prints one sen- tence telling everyone what you are learning about in this chapter. Call the function, and make sure the message displays correctly.
Write a function called favorite_book() that accepts one parameter, title. The function should print a message, such as One of my favorite books is Alice in Wonderland. Call the function, making sure to include a book title as an argument in the function call.
Write a function called make_shirt() that accepts a size and the text of a message that should be printed on the shirt. The function should print a sentence summarizing the size of the shirt and the message printed on it. Call the function once using positional arguments to make a shirt. Call the function a second time using keyword arguments.
def make_shirt(size,text):
print(f"{size} of your shirt shows how intellectual you are . {text}" )
# with positional argument
make_shirt(42,"Do u agree?")
# with keyword argument
make_shirt(size=42,text="Do u agree?")
42 of your shirt shows how intellectual you are . Do u agree?
42 of your shirt shows how intellectual you are . Do u agree?
Modify the make_shirt() function so that shirts are large by default with a message that reads I love Python. Make a large shirt and a medium shirt with the default message, and a shirt of any size with a different message.
Write a function called describe_city() that accepts the name of a city and its country. The function should print a simple sentence, such as Reykjavik is in Iceland. Give the parameter for the country a default value. Call your function for three different cities, at least one of which is not in the default country.
def describe_city(name,country):
print(f"{name} is in {country}")
describe_city("fsd","pakistan")
def describe_city(name,country='pakistan'):
print(f"{name} is in {country}")
describe_city(name='islamabad')
describe_city(name='lahore')
describe_city(name='paris')
fsd is in pakistan
islamabad is in pakistan
lahore is in pakistan
paris is in pakistan
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:
Call your function with at least three city-country pairs, and print the values that are returned.
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.
def make_album(artist_name,album_title):
artist_dic = {'artist':artist_name.title(),
'album':album_title.title()}
return artist_dic
make_album('jawad ahmad','some album')
make_album('abrar','billo')
make_album('asim','psl')
{'artist': 'Asim', 'album': 'Psl'}
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.
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.
def make_album():
while True:
artist_name = input("Please input artist name. You can press q at any time to quit program")
if artist_name == 'q':
break
album_title = input("\nPlease input album name. You can press q at any time to quit program")
if album_title == 'q':
break
songs = input("\nPlease input number of songs. You can press q at any time to quit program")
if songs == 'q':
break
artist_dic = {'artist':artist_name.title(),
'album':album_title.title(),
'Number of songs': songs}
return artist_dic
dict = make_album()
print(dict)
From here onwards 3 collaborators (Me, Malik Hasan and Ujala Tasneem) worked on the exercises. Check out Malik’s blog on this link
Make a list containing a series of short text messages. Pass the list to a function called show_messages(), which prints each text message.
Start with a copy of your program from Exercise 8-9. Write a function called send_messages() that prints each text message and moves each message to a new list called sent_messages as it’s printed. After calling the function, print both of your lists to make sure the messages were moved correctly.
Start with your work from Exercise 8-10. Call the function send_messages() with a copy of the list of messages. After calling the function, print both of your lists to show that the original list has retained its messages.
message = ["hi","bye","whats up"]
sent_messages = []
def send_messages(message):
while message:
msg = message.pop()
print(message)
sent_messages.append(msg)
print(sent_messages)
send_messages(message[:])
print(message)
['hi', 'bye']
['whats up']
['hi']
['whats up', 'bye']
[]
['whats up', 'bye', 'hi']
['hi', 'bye', 'whats up']
Write a function that accepts a list of items a person wants on a sandwich. The function should have one parameter that collects as many items as the function call provides, and it should print a summary of the sand- wich that’s being ordered. Call the function three times, using a different num- ber of arguments each time.
def requirements_for_sandwich(*ingredients):
print(f"You have following items ordered in sandwich {ingredients[1]}, {ingredients[0]}")
requirements_for_sandwich("raita","salad")
requirements_for_sandwich("anda","russian salad")
requirements_for_sandwich("cheese","fried egg","cream")
You have following items ordered in sandwich salad, raita
You have following items ordered in sandwich russian salad, anda
You have following items ordered in sandwich fried egg, cheese
Start with a copy of user_profile.py from page 148. Build a profile of yourself by calling build_profile(), using your first and last names and three other key-value pairs that describe you.
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('Shah', 'Nawaz',
location='France',
field='civil',
halat = 'bhook')
print(user_profile)
{'location': 'France', 'field': 'civil', 'halat': 'bhook', 'first_name': 'Shah', 'last_name': 'Nawaz'}
Write a function that stores information about a car in a dictionary. The function should always receive a manufacturer and a model name. It should then accept an arbitrary number of keyword arguments. Call the function with the required information and two other name-value pairs, such as a color or an optional feature. Your function should work for a call like this one:
car = make_car(‘subaru’, ‘outback’, color=‘blue’, tow_package=True)
Print the dictionary that’s returned to make sure all the information was stored correctly.
def car(manufacturer,model,**info):
manufacturer = manufacturer.title()
model = model.title()
info['manufacturer'] = manufacturer
info['model'] = model
print(info)
car('ferrari', 'SF70', color = 'red', chassis='air drawn')
{'color': 'red', 'chassis': 'air drawn', 'manufacturer': 'Ferrari', 'model': 'Sf70'}
Put the functions for the example printing_models.py in a separate file called printing_functions.py. Write an import statement at the top of printing_models.py, and modify the file to use the imported functions.
Using a program you wrote that has one function in it, store that function in a separate file. Import the function into your main program file, and call the function using each of these approaches:
import module_name from module_name import function_name from module_name import function_name as fn import module_name as mn from module_name import *
printing function is the .py file which has the code for car fucntin above.
import printing_functions
from printing_functions import car
from printing_functions import car as cr
import printing_functions as pf
from printing_functions import *
printing_functions.car('ferrari', 'SF70', color = 'red', chassis='air drawn')
{'color': 'red', 'chassis': 'air drawn', 'manufacturer': 'Ferrari', 'model': 'Sf70'}
Choose any three programs you wrote for this chapter, and make sure they follow the styling guidelines described in this section.
already did that