Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- #2D Array to store the users and passwords when the program is running
- usersArray = []
- def loadFile():
- usersFile = open("users.txt", "r")
- for line in usersFile:
- user = []
- user = line.split(",")
- user[len(user)-1] = user[len(user)-1].strip("\n")
- usersArray.append(user)
- usersFile.close()
- def signUp():
- #Ask the user for their name
- firstName = input("Please enter your first name\n")
- surname = input("Please enter your surname\n")
- #Generate the first part of the username
- username = firstName + surname
- #Concatenate 3 random numbers at the end
- counter = 0
- while counter < 3:
- randomNumber = random.randint(0,9)
- username = username + str(randomNumber)
- counter = counter + 1
- #Ask for the password two times
- password = input("Please enter your password\n")
- password1 = input("Please re-enter your password\n")
- #Continue the program if the passwords match
- if password == password1:
- print("Passwords match")
- else:
- print("Passwords don't match")
- exit()
- print("Your username is " + username + " please make a note of this")
- user = []
- user.append(username)
- user.append(password)
- usersArray.append(user)
- print(usersArray)
- usersFile = open("users.txt", "w")
- for user in usersArray:
- lineForFile = ""
- for item in user:
- if item == user[len(user)-1]:
- lineForFile = lineForFile + item + "\n"
- else:
- lineForFile = lineForFile + item + ","
- print(lineForFile)
- usersFile.write(lineForFile)
- usersFile.close()
- def login():
- LoggedIn = False
- while not LoggedIn:
- enteredUsername = input("Enter your username\n")
- enteredPassword = input("Enter your password\n")
- for user in usersArray:
- if enteredUsername == user[0] and enteredPassword == user[1]:
- print("Login sucessful")
- LoggedIn = True
- if not LoggedIn:
- print("username or password incorrect")
- '''Main Program'''
- loadFile()
- running = True
- while running:
- menuChoice = input("Menu: \n1. Sign up\n2. Login\n")
- if menuChoice == "1":
- signUp()
- elif menuChoice == "2":
- login()
- answer = input("Do you want to run the program again? y/n\n")
- if answer == "y":
- running = True
- elif answer == "n":
- running = False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement