《Python编程实践》动手试一试答案。

快乐又不要钱,为什么不呢?

【注】本文中所有的代码本人全部测试通过后才写在下面。考虑到夹杂结果图片太乱,所以没加。大家复制代码拿去运行即可。

前面章节的答案后续会补上。

持续更新……

《Python编程实践》动手试一试答案。_第1张图片 

guests=['admin','Z','W','L','Z']
for guest in guests:
	if guest=='admin' :
		print("love you my admin~~")
	else :
		print("welcome home "+guest +".")

guests=[]

if guests:
	for guest in guests:
		if guest=='admin' :
			print("love you my admin~~")
		else :
			print("welcome home "+guest +".")
else:
	print("We need to find some users!")

 《Python编程实践》动手试一试答案。_第2张图片

 

current_users=['A','B','C','D','E']
new_users=['A','B','X','Y','S']
for new_user in new_users:
	if new_user in current_users:
		print('Sorry ,you need to change your name!')
	else:
		print('This name has never been used!')

 【做这一问时遇到了问题,我先把正解写在下面再把我错误那个 拿出来分析】

#这是正解 
current_users=['A','B','C','D','E']
curren_users=[x.swapcase() for x in current_users]

new_users=['A','B','X','S','Y']

for user in new_users:
    if user.lower() in current_users:
        print('Sorry ,this name has been used ')
    else :
        print('Ok ,you have a new name')
#这是有问题的代码 

current_users=['A','B','C','D','E']
#我主要错在这个for 循环,我想把列表中所有元素变小写。
for current_user in current_users:
	store=current_user.lower()
	current_users.append(store)
	current_users.remove(store)
	
print(current_users)

new_users=['a','B','X','Y','S']
for user in new_users:
	if user.lower() in current_users:
		print('Sorry ,you need to change your name!')
	else:
		print('This name has never been used!')

错误分析:
就是,for 循环是始终按索引+1去遍历,在删除了第一个元素之后,我想要遍历的第二个元素变成了第一个元素,但此时for 循环认为已经遍历完了第一个,开始遍历第二个。此时的第二个就是没删除元素时的第三个元素。所以我看到的是跳了一个的结果 

 

你可能感兴趣的:(python,pytorch)