Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. from random import *
  2. import csv
  3. import string
  4. def menu_selector():
  5. #gets user input and validates it.
  6. invalid_input = True
  7. while invalid_input:
  8. try:
  9. user_choice = int(input('Choose an option'))
  10. if user_choice == 1:
  11. #print('Register was selected')
  12. register_user()
  13. invalid_input = False
  14. elif user_choice == 2:
  15. #print('Login was selected')
  16. login()
  17. invalid_input = False
  18. elif user_choice == 3:
  19. #print('Quit was selected')
  20. invalid_input = False
  21. exit()
  22. else:
  23. print('Only numbers 1, 2 or 3 can be entered')
  24. except ValueError:
  25. print('Only numbers 1, 2 or 3 can be entered')
  26.  
  27. def loginMenu_selector(username):
  28. #gets user input and validates it.
  29. invalid_input = True
  30. while invalid_input:
  31. try:
  32. user_choice = int(input('Choose an option'))
  33. if user_choice == 1:
  34. print('Quiz was selected')
  35. register_user()
  36. invalid_input = False
  37. elif user_choice == 2:
  38. print('Admin was selected')
  39. if username == "Fergus":
  40. admin()
  41. else:
  42. print("You are not fergus")
  43. invalid_input = False
  44. elif user_choice == 3:
  45. print('Quit was selected')
  46. invalid_input = False
  47. exit()
  48. else:
  49. print('Only numbers 1, 2 or 3 can be entered')
  50. except ValueError:
  51. print('Only numbers 1, 2 or 3 can be entered')
  52.  
  53. def register_user():
  54. print('Register user invoked')
  55. write_user_data()
  56.  
  57.  
  58. def main_menu():
  59. #displays main menu
  60. title = '''
  61. Welcome to the Quiz
  62. --------------------'''
  63. menu = '''
  64. 1. Register
  65. 2. Login
  66. 3. Quit'''
  67.  
  68. print(title)
  69. print()
  70. print(menu)
  71. menu_selector()
  72.  
  73. def loginmenu(username):
  74. title = 'Welcome '+ username
  75. deco = '-'
  76.  
  77. menu = '''
  78. 1. Quiz
  79. 2. Manage (Admin Only)
  80. 3. Quit'''
  81.  
  82. print(title)
  83. print(deco *len(title))
  84. print()
  85. print(menu)
  86.  
  87.  
  88. def login():
  89. with open("student_records.csv", "r") as file:
  90. read=csv.reader(file)
  91. invalid_login = True
  92. while invalid_login == True:
  93. username = input ('What is your username')
  94. password = input ('What is your password')
  95. for each in read:
  96. if username in each and password in each:
  97. print ('record was found')
  98. invalid_login = False
  99.  
  100. else:
  101. break
  102. print('Incorrect username or password')
  103. loginmenu(username)
  104.  
  105. def get_validate_userdata():
  106. missing_data = True
  107. while missing_data:
  108. name = input ('Enter your name: ')
  109. surname = input ('Enter your surname: ')
  110. age = input ('Enter your age')
  111. group = input ('Enter your year group: ')
  112. password = password_check()
  113. if name == '' or surname == '' or group == '' or password == '' or age == '':
  114. print ('Fields cannot be blank')
  115. else:
  116. missing_data = False
  117. username =str(randint(10,99))+name[0:2]+surname[0:2]
  118. return (username, name, surname, group, age, password)
  119. break
  120.  
  121. def password_check():
  122. incorrect_data = True
  123. while incorrect_data:
  124. password = input ('Enter your password: ')
  125. for each in password:
  126. if each in string.ascii_uppercase:
  127. print('')
  128. incorrect_data = False
  129. break
  130. else:
  131. print ('Weak password, Your password must contain a capital letter')
  132. return password
  133.  
  134. def write_user_data():
  135. data = get_validate_userdata()
  136. data_to_write = []
  137. for each in data:
  138. data_to_write.append(each)
  139. print (data_to_write)
  140. with open ('student_records.csv','a') as studentFile:
  141. studentFileWriter = csv.writer(studentFile)
  142. studentFileWriter.writerow(data_to_write)
  143. print('Data written successfully')
  144. studentFile.close()
  145. main_menu()
  146.  
  147. main_menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement