在这里是初学者程序员,我正在尝试制作一个程序,该程序可以用作注册和检查所估算的用户名是否已被使用的方式。我的代码不会产生任何错误,但是如果使用了用户名,则IF语句始终会被忽略,并且不会产生错误消息。
username = input("Username: ")
def register():
with open("Usernames.txt", "r+") as f:
f.write("\n")
for line in f:
if line == username :
print("Error")
else:
f.write(username)
register()
input
strips the newline from input it reads; a file iterator does not.You need to use
f.readlines()
in your code