Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. """ Joey Turner 10/7/2017
  2. Programming Project
  3. """
  4.  
  5. def get_inputs():
  6. """ This function will get the user to enter in all the inputs
  7. needed to run the program, and then return those values to the main
  8. program.
  9. """
  10.  
  11. firstname = input("Please enter your first name > ")
  12. # Asks user to enter their first name
  13. surname = input("Please enter your surname > ")
  14. # Asks user to enter their surname
  15. gender = input("Please enter your gender, M or F > ")
  16. # Asks user to enter their gender
  17. return firstname, surname, gender
  18. # Returns the variables 'firstname', 'surname' and 'gender' to the
  19. # main program.
  20.  
  21.  
  22.  
  23. def validate_gender(gender):
  24. """ This function will validate whether the user has entered just ‘M’ or
  25. ‘F’ for the gender.
  26. """
  27.  
  28. while gender != "M" and gender != "F":
  29. # Iterates this section of the function until gender is equal to
  30. # either 'M' or 'F'
  31. print("Invalid selection of gender.")
  32. # Notifies the user of their mistake
  33. gender = input("Please re-enter your gender, M or F > ")
  34. # Asks the user to re-enter their gender
  35. return gender
  36. # Returns the variable 'gender' to the main program.
  37.  
  38.  
  39.  
  40. def get_user(firstname,surname,gender):
  41. """ This function will create the username for the user.
  42.  
  43. If gender==M, then username is last 3 letters of surname + first 2
  44. letters of first name
  45. If gender == F, then username is first 3 letters of first name +
  46. first 2 letters of surname
  47. """
  48.  
  49. if gender == "F":
  50. # Checks whether gender = F
  51. username = firstname[0:3] + surname[0:2]
  52. # Username is assigned as the first 3 letters of the firstname, and
  53. # the first 2 letters of the surname
  54. else:
  55. # If username is not equal to F (should only be M after the
  56. # validate_gender function), username is assigned as the last 3
  57. # letters of the surname and the first 2 letters of the firstname.
  58. username = surname[-3:] + firstname[0:2]
  59. return username
  60.  
  61.  
  62.  
  63. def file_append(username):
  64. """ This subprogram will append the username and password to the text file
  65. ‘logins.txt’
  66. """
  67.  
  68. myFile = open('logins.txt','a')
  69. # The file is opened in append mode, meaning anything written in this
  70. # procedure will not overwrite current data stored on the file.
  71. password = "Password123\n"
  72. # the variable password is set to 'Password123' so that it can be added
  73. # to the record.
  74. record = "Username: " + username + " " + "Password: " + password
  75. # Combines username and password (along with a spacer) into one variable
  76. # so it can be appended to the file more easily
  77. myFile.write(record)
  78. # The variable 'record' is appended to the file
  79. myFile.close
  80. # File is closed so that other programs or the user can now open it.
  81.  
  82.  
  83.  
  84. def main():
  85. """ Displays welcome and goodbye messages, calls all other subprograms.
  86. """
  87.  
  88. print("=-=-= WELCOME TO THE USERNAME ASSIGNER! =-=-=")
  89. # Welcomes the user to the program
  90. firstname, surname, gender = get_inputs()
  91. # Calls the get_inputs function, defining the 3 variables above as
  92. # whatever the subprogram will return.
  93. validate_gender(gender)
  94. # Calls the validate_gender function, passing in the variable 'gender'
  95. # from the get_inputs function
  96. username = get_user(firstname,surname,gender)
  97. # Calls the get_user function, passing in 3 variables and defining the
  98. # variable 'username' as whatever the program returns
  99. file_append(username)
  100. # Calls the file_append procedure, passing in the variable 'username'
  101. print("Username generated at 'logins.txt': {0}".format(username))
  102. # Displays message to show user that the username has been successfully
  103. # generated, and then displays the username.
  104.  
  105. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement