I am relatively new to coding and I am creating a simple login system for an assignment. I have created code so far that is able to register new users and save that data to the text file, but I am having a huge mind blank on what to do to get it to register existing users. So for example, when the user goes to register, if the username already exists it stops them from proceeding; as well as when they go to login, it recognises them in the system. Not sure if I am over thinkiung things but if someone could point me in the right direction I would very much appreciate it!!! Thank you!
import string
import time
import secrets
file = open('accounts.txt.txt', 'a+')
master = 'admin:password'
#random password generation
def generate_random_password(length=10, use_uppercase=True, use_lowercase=True, use_digits=True, use_punctuation=True):
character_sets = []
if use_uppercase:
character_sets.append(string.ascii_uppercase)
if use_lowercase:
character_sets.append(string.ascii_lowercase)
if use_digits:
character_sets.append(string.digits)
if use_punctuation:
character_sets.append(string.punctuation)
if not character_sets:
raise ValueError("At least one character set must be selected.")
alphabet = ''.join(character_sets)
password = ''.join(secrets.choice(alphabet) for i in range(length))
return password
print("Welcome! Please choose one of the below options:")
#Home screen/menu
print("bA. Login")
print("bB. Register")
print("bC. View Accounts")
print("bD. Exit")
choice = input("Enter your choice (A/B/C/D): ").upper()
choice = choice.upper()#this should allow both lower and upper case letters
if choice == "A":
username = input("Please enter your username: ")
password = input("Please enter your password: ")
if username in file:#TBC - need to have code recognise username and password data in file
print("Login Successful! Welcome " + username + "!")
else:
print("Invalid username or password! Please try again.")
elif choice == "B":
lst = []
username = input("Please enter a username: ")
find = read.file
for lines in find:
if username == lines[0]:
print("Username already in use. Please create another.")
lst.append(username + ' ')
generate_new_password = input("Do you want to generate a random password? (yes/no): ").lower()
if generate_new_password == 'yes':
if generate_new_password == "yes":
# Password generation settings
use_uppercase = input("Include uppercase letters? (yes/no): ").lower() == "yes"
use_lowercase = input("Include lowercase letters? (yes/no): ").lower() == "yes"
use_digits = input("Include digits? (yes/no): ").lower() == "yes"
use_punctuation = input("Include punctuation? (yes/no): ").lower() == "yes"
password_length = int(input("Enter password length (minimum 10): ")) # Minimum length requirement
try:
password = generate_random_password(
length=password_length,
use_uppercase=use_uppercase,
use_lowercase=use_lowercase,
use_digits=use_digits,
use_punctuation=use_punctuation
)
print(f"Generated password: {password}")
except ValueError as e:
print(e)
#Password Creation______________________________________________Updates the text file only if password > 10
else:
password = input("Create your password: ")
if len(password) < 10:
print("Password must be at least 10 characters long.")
if len(password) >= 10:
print("User details registered!")
lst.append(password + 'n')
file.writelines(lst)
file.close()
elif choice == "C": #View Accounts menu for admin only
print("To view customer account information, please enter administration details below")
username = input("Please enter admin username: ")
password = input("Please enter admin password: ")
if username in master and password in master:
print("Welcome " + username + "!")
print(file.read())
elif choice == "D":#Exit program
print("Exiting program...")
time.sleep(2)#2 second time delay
print("Thank you! See you again soon!")
file.close()
else:
print("Invalid choice. Please try again.")
Skye is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.