Python入门习题大全——索引
创建一个至少包含5个用户名的列表, 且其中一个用户名为“admin”。 想象你要编写代码,在每位用户登录网站后都打印一条问候消息。
遍历用户名列表,并向每位用户打印一条问候消息。
如果用户名为"admin’ ,就打印一条特殊的问候消息,如"Hello admin, would you like to see a status report?"。
否则,打印一条普通的问候消息,如"Hello Eric, thank you for lgging in again"。
# 以特殊的方式跟管理员打招呼
users = ['ling', 'hui', 'he', 'zxx', 'admin']
for user in users:
if user == 'admin':
print("Hello admin, would you like to see a status report?")
else:
print("Hello " + user + ", thank you for logging in again.")
输出为:
Hello ling, thank you for logging in again.
Hello hui, thank you for logging in again.
Hello he, thank you for logging in again.
Hello zxx, thank you for logging in again.
Hello admin, would you like to see a status report?