gpt4 book ai didi

python - 为什么不打印字符串 'andy' 请输入新用户名?

转载 作者:行者123 更新时间:2023-12-01 06:22:01 24 4
gpt4 key购买 nike

current_users = ['Andy', 'Brett', 'Cassie', 'Derek', 'Eric']
new_users = ['eric', 'greg', 'hank', 'ian', 'john', 'andy', 'frank']
new_users.sort()

for current_user in current_users:
current_user = current_user.lower()

for new_user in new_users:
if new_user == current_user:
print(f"\n{new_user}, Please enter a new username!")
else:
print(f"\n{new_user}, Username is available.")

由于用户名可用,Andy 正在被打印。另外请帮我简化一下,因为我刚刚学习 python。

最佳答案

您可以使用列表理解current_users中的用户名转换为小写。其次,您必须检查 new_user 是否已存在于 current_users 中,为此您必须使用 in 关键字。

in 关键字

tests whether or not a sequence contains a certain value.

这是代码,

current_users = ['Andy', 'Brett', 'Cassie', 'Derek', 'Eric']
new_users = ['eric', 'greg', 'hank', 'ian', 'john', 'andy', 'frank']
new_users.sort()

current_users = [user.lower() for user in current_users]

for new_user in new_users:
if new_user in current_users:
print(f"\n{new_user}, Please enter a new username!")
else:
print(f"\n{new_user}, Username is available.")

希望对你有帮助!

关于python - 为什么不打印字符串 'andy' 请输入新用户名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60312954/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com