第四周作業(7,8章)

7-1

#coding:gbk
#7-1汽車租賃

car = input("你想租什麼牌子的汽車:")
print("我給你找找有沒有" + car)

7-2

#coding:gbk
#7-2餐館訂位

num_of_people = input("有多少人在用餐:")
if int(num_of_people) > 8:
	print("沒有空位了。")
else:
	print("還有空位。")

7-3

#coding:gbk
#7-3 10的倍數

n = input("Please enter a number: ")
if int(n) % 10 == 0:
	print(n + ' is a multiple of 10.')
else:
	print(n + ' is not a multiple of 10.')

7-4

#coding:gbk
#7-4披薩配料

information = "請輸入你想加入的配料,輸入quit結束:"
a = ''
while(a != 'quit'):
	a = input(information)
	if a != 'quit':
		print("成功添加" + a)

7-5

#coding:gbk
#7-5電影票

age = ''

while True:
	age = input("請輸入你的年齡(輸入quit退出): ")
	if age == 'quit':
		break
	else:		
		if int(age) < 3:
			print("免費。")
		elif int(age) <= 12:
			print("票價爲10美元。")
		else:
			print("票價爲15美元.")

7-8

#coding:gbk
#7-8熟食店

sandwich_orders = ['s1', 's2', 's3']
finished_sandwiches = []

while sandwich_orders:
	current_sw = sandwich_orders.pop()
	print(' I made your ' + current_sw + ' sandwich')
	finished_sandwiches.append(current_sw)

print(finished_sandwiches)
	
	

8-1

#coding:gbk
#8-1 消息

def display_message():
	print("本章學習的是函數相關的內容")

display_message()

8-2

#coding:gbk
#8-2 喜歡的圖書

def favorite_book(title):
	print(" One of my favorite books is " + title)


favorite_book('Alice in Wonderland')	

8-3

#coding:gbk
#8-3 T恤


def  make_shirt(size, word = 'I love Python'):
	#尺碼有小碼S,中碼M,大碼L
	print('這件T恤的尺碼是' + size + ', 需要打印的字樣是:' + word)


make_shirt('M', 'J love T') 

8-4

#coding:gbk
#8-4 大號T恤

def  make_shirt(size = 'L', word = 'I love Python'):
	#尺碼有小碼S,中碼M,大碼L
	print('這件T恤的尺碼是' + size + ', 需要打印的字樣是:' + word)

make_shirt()
make_shirt('M')
make_shirt(word = 'J love T') 

8-5

#coding:gbk
#8-5 城市

def describe_city(name, country = 'China',):
	print(name + 'is in ' + country)

describe_city('Shanghai')
describe_city('Dandong')
describe_city('NewYork', 'America')

8-6

#coding:gbk
#8-6城市名

def city_country(city_name, country):
	s = '"' + city_name + ', ' + country + '"'
	return s

s1 = city_country('Shanghai', 'China')
print(s1)

8-7~8-8

#coding:gbk
#8-7 專輯

def make_album(singer, album_name):
	a = {'singer':singer, 'album_name':album_name}
	return a

a = make_album('Timmy', 'Light')
print(a)

b = make_album('Timmy', '15Minute')
print(b)

c = make_album('Timmy', 'Time')
print(c)

#8-8 用戶的專輯
while(True):
	s1 = input("請輸入歌手姓名(輸入‘quit’結束程序): ")
	if s1 == 'quit':
		break;
		
	s2 = input("請輸入專輯名稱(輸入‘quit’結束程序): ")
	if s2 == 'quit':
		break;
		
	print(make_album(s1, s2))
	

8-9~8-11

#coding:gbk
#8-9 魔術師

def show_magicians(magicians):
	for magician in magicians:
		print(magician)

magicians = ['Johnny', 'Tom', 'Jerry', 'Timmy']
show_magicians(magicians)
print('--------------------------------------\n')

#8-10 了不起的魔術師
def make_great(magicians):
	length = len(magicians)
	for a in range(0, length):
		magicians[a] = 'the Great ' + magicians[a]	
	return magicians


'''
【遇到的問題】
def make_great(magicians):
	for a in magicians: #這樣只是修改了副本a,沒有修改列表裏的元素
		a = 'the Great ' + a
		print(a)
'''	
	
make_great(magicians)
show_magicians(magicians)
print('--------------------------------------\n')

#8-11 不變的魔術師 
great_magi = make_great(magicians[:])
show_magicians(great_magi)
show_magicians(magicians)

8-12

#coding:gbk
#8-12 三明治

def make_san(*materials):
	print("Making a sandwich with the following materials:")
	for material in materials:
		print('-' + material)
	
make_san('a', 'b', 'c')
make_san('d', 'e')
make_san('k')

8-13

#coding:gbk
#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('Lily', 'G',
							location='princeton',
							field='physics')
print(user_profile)

8-14

#coding:gbk
#8-14 汽車

def make_car(manufac, ver, **info):
	car = {}
	car['manufacturer'] = manufac
	car['version'] = ver
	
	for key, value in info.items():
		car[key] = value
		
	return car
	
car = make_car('subaru', 'outback', color='blue', tow_package=True)
print(car)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章