最近的博文:
Python基础入门例程44-NP44 判断列表是否为空(条件语句)-CSDN博客
Python基础入门例程43-NP43 判断布尔值(条件语句)-CSDN博客
Python基础入门例程42-NP42 公式计算器(运算符)-CSDN博客
目录
最近的博文:
描述
输入描述:
输出描述:
解答:
说明:
描述
创建一个依次包含字符串'Niuniu'、'Niumei'、'GURR'和'LOLO'的列表current_users,
再创建一个依次包含字符串'GurR'、'Niu Ke Le'、'LoLo'和'Tuo Rui Chi'的列表new_users,
使用for循环遍历new_users,如果遍历到的新用户名在current_users中,
则使用print()语句一行输出类似字符串'The user name GurR has already been registered! Please change it and try again!'的语句,
否则使用print()语句一行输出类似字符串'Congratulations, the user name Niu Ke Le is available!'的语句。(注:用户名的比较不区分大小写)
输入描述:
无
输出描述:
按题目描述进行输出即可。
The user name GurR has already been registered! Please change it and try again!
Congratulations, the user name Niu Ke Le is available!
The user name LoLo has already been registered! Please change it and try again!
Congratulations, the user name Tuo Rui Chi is available!
解答:
current_users = ['Niuniu','Niumei','GURR','LOLO']
new_users = ['GurR','Niu Ke Le','LoLo','Tuo Rui Chi']
#转换为小写
#lower_current_users=list(map(str.lower,current_users))
lower_current_users = [user.lower() for user in current_users]
#循环查找
for new_user in new_users:if new_user.lower() in lower_current_users:print(f"The user name {new_user} has already been registered! \
Please change it and try again!")else:#print(f"Congratulations, the user name {new_user} is available!")print("Congratulations, the user name {} is available!".format(new_user))
说明:
考查了循环语句的使用、以及字符比较时统一转换为小写的操作
两种写法:
for new_user in new_users:
lower_current_users = [user.lower() for user in current_users]
注意:
分行符\表示代码在换行后仍然属于同一行
字符串插值(f-string)。它的输出是一个字符串,包括了字符串变量new_user的值
或者使用format函数打印字符串