Advertisement
Guest User

Course work

a guest
Dec 7th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. import random
  2. #2D Array to store the users and passwords when the program is running
  3. usersArray = []
  4.  
  5. def loadFile():
  6. usersFile = open("users.txt", "r")
  7.  
  8. for line in usersFile:
  9. user = []
  10. user = line.split(",")
  11.  
  12. user[len(user)-1] = user[len(user)-1].strip("\n")
  13.  
  14. usersArray.append(user)
  15.  
  16. usersFile.close()
  17.  
  18. def signUp():
  19. #Ask the user for their name
  20. firstName = input("Please enter your first name\n")
  21. surname = input("Please enter your surname\n")
  22.  
  23. #Generate the first part of the username
  24. username = firstName + surname
  25.  
  26. #Concatenate 3 random numbers at the end
  27. counter = 0
  28. while counter < 3:
  29. randomNumber = random.randint(0,9)
  30. username = username + str(randomNumber)
  31. counter = counter + 1
  32.  
  33. #Ask for the password two times
  34. password = input("Please enter your password\n")
  35. password1 = input("Please re-enter your password\n")
  36.  
  37. #Continue the program if the passwords match
  38. if password == password1:
  39. print("Passwords match")
  40. else:
  41. print("Passwords don't match")
  42. exit()
  43.  
  44. print("Your username is " + username + " please make a note of this")
  45.  
  46. user = []
  47. user.append(username)
  48. user.append(password)
  49. usersArray.append(user)
  50. print(usersArray)
  51.  
  52. usersFile = open("users.txt", "w")
  53.  
  54. for user in usersArray:
  55. lineForFile = ""
  56.  
  57. for item in user:
  58. if item == user[len(user)-1]:
  59. lineForFile = lineForFile + item + "\n"
  60. else:
  61. lineForFile = lineForFile + item + ","
  62.  
  63. print(lineForFile)
  64. usersFile.write(lineForFile)
  65.  
  66. usersFile.close()
  67.  
  68. def login():
  69. LoggedIn = False
  70.  
  71. while not LoggedIn:
  72.  
  73. enteredUsername = input("Enter your username\n")
  74. enteredPassword = input("Enter your password\n")
  75.  
  76. for user in usersArray:
  77. if enteredUsername == user[0] and enteredPassword == user[1]:
  78. print("Login sucessful")
  79. LoggedIn = True
  80. if not LoggedIn:
  81. print("username or password incorrect")
  82.  
  83.  
  84.  
  85.  
  86.  
  87. '''Main Program'''
  88.  
  89. loadFile()
  90.  
  91. running = True
  92.  
  93. while running:
  94. menuChoice = input("Menu: \n1. Sign up\n2. Login\n")
  95.  
  96. if menuChoice == "1":
  97. signUp()
  98. elif menuChoice == "2":
  99. login()
  100.  
  101.  
  102. answer = input("Do you want to run the program again? y/n\n")
  103.  
  104. if answer == "y":
  105. running = True
  106. elif answer == "n":
  107. running = False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement