文章

Python 从入门到实践 第八章 函数 8-9-11习题








#coding:gbk
#传递列表
def greet_users(names):
	for name in names: #用name遍历names
		msg = 'Hello '+name.title()+'!'
		print(msg)
usernames = ['hanmeimei','lilei','daxiong'] #定义列表usernames
greet_users(usernames)                      #调用函数 greet_users,并将usernames列表传递给它

print('\n')

unprinted_designs = ['iphone case','robot pendant','dodecahedron']
complete_models = []

while unprinted_designs: #非空列表为True
	current_design = unprinted_designs.pop()
	print('printing model: '+current_design)
	complete_models.append(current_design)
print('\nthe following models have been printed: ')
for completed_model in complete_models:
	print (completed_model)
	
	
print('\n')

def print_models(unprinted_designs,complete_models):
	
	while unprinted_designs:
		current_design = unprinted_designs.pop()
		print('printing model: '+current_design)
		complete_models.append(current_design)

def show(complete_models):
	print('\nthe following model have been printed: ')
	for complete_mode in complete_models:
		print(complete_mode)

unprinted_designs = ['iphone case','robot pendant','dodecahedron']
complete_models = []
print_models(unprinted_designs,complete_models)
show(complete_models)
print('\n')
#习题8-9-11

def show_magicians(magicians):
	for magician in magicians:
		print('hello '+magician+'.')

 #第一个一次写完就编译通过代码 卡在下面两函数题了,今天出不来,先看了下答案
def make_great(magicians):
	great_magicians = []
	while magicians:
		magician = magicians.pop()
		great_magician = magician+' the Great'
		great_magicians.append(great_magician)	
		
	for great_magician in great_magicians:
		magicians.append(great_magician)
		
	return magicians
magicians = ['Harry Houdini', 'David Blaine', 'Teller']
show_magicians(magicians)

print("\nGreat magicians:")
great_magicians = make_great(magicians[:])
show_magicians(great_magicians)

print("\nOriginal magicians:")
show_magicians(magicians)

下面是新手的一个问题库,我自己画的,自己感觉很绕

 

凌晨2点半了,大概理解了,明天继续打豆豆

来看看注绎

#习题8-9-11

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

 #第一个一次写完就编译通过代码 卡在下面两函数题了,半天才出来
def make_great(magicians):  #定义函数,magicians是个数组,实参传递给了这里的形参,题目需要不修改原始列表 被for循环加入了the great
	great_magicians = []	#一个magicians列表的副本,循环后被加了 the great.
	while magicians:		#开始循环 magicians内非空的时候为True
		magician = magicians.pop() #删除magicians的最后一个元素并赋给magician
		great_magician = magician+' the Great'  #magician得到元素后添加字符串,赋给了great_magician
		great_magicians.append(great_magician)	#加好的the great 循环把所有元素给了 great_magicians列表
		
	for great_magician in great_magicians: #用great_magician 遍历加好了the great的great_magicians列表
		magicians.append(great_magician)   #把加好了 the great的所有内容加入magicians
		
	return magicians #返回带有the great的新名字给函数代码行,现在 函数代码行/magicians/great_magicians 都是添加了the great的
magicians = ['Harry Houdini', 'David Blaine', 'Teller']
show_magicians(magicians)

print("\nGreat magicians:")
great_magicians = make_great(magicians[:])
show_magicians(great_magicians)#测试后发现,打印是通过show_magicians的,传入实参为 great_magicians
								#好像是因为这个函数内有print,每次调用,函数show_magicians都运行一次
print("\nOriginal magicians:")#原始 magicians
show_magicians(magicians)#实参为原始的magicians

这道题我从遍历 for xx in xx 开始卡

然后尝试使用我自己的逻辑去解题,下面是第一种解题思维,且已经证明为正确的

可是我还是没搞懂 他遍历完之后为什么不直接打印 

而是要把great_magician appeend给magicians,也不明白为什么要return

特别是return是干嘛用的

#coding:gbk
def show_magicians(magicians):
	for magician in magicians:
		print(magician)


def make_great(magicians):  #定义函数,magicians是个数组,实参传递给了这里的形参,题目需要不修改原始列表 被for循环加入了the great
	great_magicians = []	#一个magicians列表的副本,循环后被加了 the great.
	while magicians:		#开始循环 magicians内非空的时候为True
		magician = magicians.pop() #删除magicians的最后一个元素并赋给magician
		great_magician = magician+' the Great'  #magician得到元素后添加字符串,赋给了great_magician
		great_magicians.append(great_magician)	#加好的the great 循环把所有元素给了 great_magicians列表
	for great_magician in great_magicians:
		print(great_magician)
#	for great_magician in great_magicians: #用great_magician 遍历加好了the great的great_magicians列表
#		magicians.append(great_magician)   #把加好了 the great的所有内容加入magicians
		
#	return magicians #返回带有the great的新名字给函数代码行,现在 函数代码行/magicians/great_magicians 都是添加了the great的
magicians = ['Harry Houdini', 'David Blaine', 'Teller']  #给实参定义一个列表
show_magicians(magicians)   #将列表实参传递给函数show

print("\nGreat magicians:")
great_magicians = make_great(magicians[:])#列表magicians的副本为函数make_great_magicians,
#show_magicians(great_magicians)#测试后发现,打印是通过show_magicians的,传入实参为 great_magicians
								#好像是因为这个函数内有print,每次调用,函数show_magicians都运行一次
print("\nOriginal magicians:")#原始 magicians
show_magicians(magicians)#实参为原始的magicians


原文来自:Python 从入门到实践 第八章 函数 8-9-11习题,尊重自己,尊重每一个人;转发请注明来源!
0 0

发表评论

  • 评论1则,共1页,当前第1
    • 游于艺 [管理员]

      你这就是问的太乱了,return就是为了返回列表,
      可以给别的函数或者什么的使用,如果就是print那就只能打印
      遍历完magician是magicians最后一个元素,怎么可能和列表相等