我是Python新手。
管理员您好:
列出五个或更多用户名,包括姓名 '行政'。想象一下,您正在编写代码,在每个用户登录网站后向其打印问候语。循环浏览列表,并向每个用户打印问候语:
• 如果用户名是“admin”,请打印特殊的问候语,例如 Hello admin,您想查看状态报告吗?
• 否则,请打印通用问候语,例如“Hello Eric,感谢您再次登录”。
代码:
u = input('Enter Username: ').title()
for user in usernames:
if u in user == 'Admin':
print('Welcom admin')
if u in user:
print('In list')
else:
print('Not in list')
以表格
users = [user1, user2, ...]
列出清单
然后使用
for
循环迭代用户列表,并插入 if
语句来控制用户列表中的用户是否是“admin”用户以更改问候语。
username = input('Enter Username: ').title()
输入后:
for username in usernames:
if username in usernames:
if username == 'Admin'
print("Hello admin, would you like to see a status report?")
else:
print("Hello " + username + ", thank you for logging in again.")
else:
print("You are not on the user's list")
这将循环遍历数组中的所有值,如果该值等于 admin 它将打印一条特殊消息,否则打印一条通用消息:)
根据问题,您正在寻找答案
创建一个列表
user_names
(基于 PEP 8 命名约定)并循环遍历它们
user_names = ['eric', 'willie', 'admin', 'erin', 'ever']
for user in user_names:
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 eric, thank you for logging in again!
Hello willie, thank you for logging in again!
Hello admin, would you like to see a status report?
Hello erin, thank you for logging in again!
Hello ever, thank you for logging in again!
如果您只想询问名称作为输入并根据提供的输入打印输出,这里是代码
user = input('Enter Username: ').lower()
if user == 'admin':
print(f'Hello {user}, would you like to see a status report?')
else:
print(f'Hello {user.title()}, thank you for logging in again.')
使用预定义列表,您将在
user_name
列表中检查用户,这就是答案
user = input('Enter Username: ').lower()
user_names = ["eric", "admin", "lever", "matt"]
if user in user_names:
if user == 'admin':
print(f'Hello {user}, would you like to see a status report?')
else:
print(f'Hello {user.title()}, thank you for logging in again.')
else:
print('Not in list')
输出:
你的 if 语句是错误的。应该是:
for user in usernames:
if user == 'admin':
print(f'Hello {user}, would you like to see a status report?')
else:
print(f'Hello {user}, thank you for logging in again.')
以上内容没有用户输入,因为据我所知,您需要的是这个。如果您坚持要求用户输入:
u = input('Enter Username: ').title()
if u in usernames:
if u == 'admin':
print(f'Hello {u.lower()}, would you like to see a status report?')
else:
print(f'Hello {u()}, thank you for logging in again.')
else:
print('Not in list')
此外,出于效率原因,您可以考虑使用
usernames = set(usernames)
将列表转换为集合。
usernames=['fahad','riad','antano','Admin']
对于用户名中的用户名: 如果用户名.lower() == 'admin': print('管理员您好,您想查看状态报告吗') 别的: print(f"您好{username.title()},感谢您再次登录")