1. 程式人生 > >高階程式設計技術 第四周作業

高階程式設計技術 第四周作業

第七章

7-1

car = input ("What kind of cars do you want? ")
print ("Let me see if I can find you a " + car + ".")

7-2

number = input ("How many of you are coming here? ")
number = int (number)
if number > 8 :
	print ("Sorry, there are no tables available.")
else :
	print ("Ok, we have a table for you.")


7-3

number = input ("Please enter a number. ")
number = int (number)
remainder = number % 10
if remainder :
	print (str(number) + " isn't an exact multiple of 10.")
else :
	print (str(number) + " is an exact multiple of 10.")


7-4

message = "What ingredients you want to add? "
ingredients = ''
while ingredients != 'quit' :
	ingredients = input (message)
	if ingredients == 'quit' :
		break
	print (ingredients)

7-6

message = "How old are you? "
active = True
while active :
	age = input (message)
	if age == 'quit' :
		active = False
		break
	age = int (age)
	if age < 3 :
		print ("You can watch it for free.")
	elif age <= 12 :
		print ("You will need to pay for $10 for the cinema tickets.")
	else :
		print ("You will need to pay for $15 for the cinema tickets.")


 7-8

sandwich_orders = ["Tofu sandwich", "tuna sandwich", "turkey sandwich"]
finished_sandwiches = []
while sandwich_orders :
	sandwich = sandwich_orders.pop(0)
	print ("I made your tuna sandwich ," + sandwich + ".")
	finished_sandwiches.append (sandwich)
print ("\n\n")
for sandwich in finished_sandwiches :
	print (sandwich)

7-9

sandwich_orders = ["Tofu sandwich", "pastrami", "pastrami", "tuna sandwich", "turkey sandwich", "pastrami"]
finished_sandwiches = []
print ("The delicatessen sold out all the pastrami.")
while "pastrami" in sandwich_orders :
	sandwich_orders.remove ("pastrami")
while sandwich_orders :
	sandwich = sandwich_orders.pop(0)
	finished_sandwiches.append (sandwich)
print ("\n\n")
for sandwich in finished_sandwiches :
	print (sandwich)

第八章

8-1

def display_message () :
	print ("I learned how to use functions in this chapter.")
display_message ()

8-2

def favorite_book (title) :
	print ("One of my favorite books is " + title + '.')
favorite_book ("Alice in Wonderland")

8-3

def make_shirt (size, logo) :
	print ("This is a " + size + " T-shirt with " + '"' + logo + '".')
make_shirt ("large", "I love Python")
make_shirt (size = "large", logo = "I love Python")

8-4

def make_shirt (size = "large", logo = "I love Python") :
	print ("This is a " + size + " T-shirt with " + '"' + logo + '".')
make_shirt (size = "large")
make_shirt (size = "middle")
make_shirt (size = "small", logo = "I love China")

8-6

def city_country (city, country) :
	message = '"' + city + ', ' + country + '"'
	print (message)
city_country ("Santiago", "Chile")
city_country ("Beijing", "China")
city_country ("London", "Britain")

8-7

def make_album (singer, album, tracks_number = 0) :
	albums = {}
	albums["singer's name"] = singer
	albums["album's name"] = album
	if tracks_number :
		albums["tracks_number"] = tracks_number
	return albums
print (make_album ("PJ Harvey", "Stories From the City,Stories From the Sea"))
print (make_album ("Chuck Berry", "The Great Twenty-Eight"))
print (make_album ("M.I.A.", "Kala"))
print (make_album ("Brian Wilson", "SMiLE", 17))

8-8

def make_album () :
	active = True
	while active :
		albums = {}
		singers_name = input ("Please enter the singer's name:\n(enter 'q' at any time to quit)\n")
		if singers_name == 'q' :
			active = False
			break;
		albums_name = input ("Please enter the album's name:\n(enter 'q' at any time to quit)\n")
		if albums_name == 'q' :
			active = False
			break;
		tracks_number = input ("Please enter the tracks' name:\n(enter 'q' at any time to quit)\n")
		if tracks_number == 'q' :
			active = False
			break;
		albums["singer's name"] = singers_name
		albums["album's name"] = albums_name
		albums["tracks' number"] = tracks_number
		print (albums)
make_album ()

8-9

def show_magicians (list) :
	for name in list :
		print (name)
orders = ["Tom", "Jack", "Jimy"]
show_magicians (orders)

8-10

def show_magicians (lists) :
	for name in lists :
		print (name)
def make_great (lists) :
	length = len (lists)
	for index in range (0, length) :
		lists[index] = "the Great " + lists[index]
orders = ["Tom", "Jack", "Jimy"]
make_great (orders)
show_magicians (orders)

8-11

def show_magicians (lists) :
	for name in lists :
		print (name)
def make_great (lists) :
	length = len (lists)
	for index in range (0, length) :
		lists[index] = "the Great " + lists[index]
	show_magicians (lists)
orders = ["Tom", "Jack", "Jimy"]
make_great (orders[:])
show_magicians (orders)

8-12

def sandwich_toppings (*toppings) :
	print ("This sandwich is made of the following toppings: ")
	for topping in toppings :
		print ("-" + topping)
sandwich_toppings ('onions')
sandwich_toppings ('green peppers', 'bread')
sandwich_toppings ('sausage', 'Green vegetables', 'chicken')

8-13

def build_profile (first, last, **user_info) :
	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 ('Wang', 'Chen', sex = 'male', age = '20', weight = '105')
print (user_profile)

8-15

printing_functions.py

def print_models(unprinted_designs, completed_models):
	while unprinted_designs:
		current_design = unprinted_designs.pop()
		print("Printing model: " + current_design)
		completed_models.append(current_design)
def show_completed_models(completed_models):
	print ("\nThe following models have been printed:")
	for completed_model in completed_models:
		print(completed_model)

printing_functions.py

from printing_functions import *
unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
completed_models = []
print_models(unprinted_designs, completed_models)
show_completed_models(completed_models)