I have a list of usernames. Only usernames that aren't already in the text file should be added. Issue is that usernames that are already in there are getting added. I am printing the list to make sure it's working. Getting this error: Redeclared 'myList' defined above without usage.
myList = []
with open('list.txt', 'r') as file:
myList = file.readlines()
print(myList)
username = input("Enter username: ")
if username not in myList:
with open("list.txt", 'a ') as output:
output.write(str(username) "\n")
print(f"Successfully added {username} to list")
else:
print(f"{username} already exists in list")
CodePudding user response:
I'm pretty sure that this line
myList = file.readlines()
returning with \n
every line
so change it to
myList = [line.rstrip() for line in file.readlines()]