Advertisement
Guest User

Untitled

a guest
Mar 16th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 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_user()
  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 register_user():
  28. print('Register user invoked')
  29. write_user_data()
  30.  
  31. def login_user():
  32. print('Login user invoked')
  33. invalid_login = True
  34. while invalid_login:
  35. username = input ('What is your username')
  36. password = input ('What is your password')
  37.  
  38. def main_menu():
  39. #displays main menu
  40. title = '''
  41. Welcome to the Quiz
  42. --------------------'''
  43. menu = '''
  44. 1. Register
  45. 2. Login
  46. 3. Quit'''
  47.  
  48. print(title)
  49. print()
  50. print(menu)
  51. menu_selector()
  52.  
  53. def loginmenu(username):
  54. title = 'Welcome '+ username
  55. deco = '-'
  56.  
  57. menu = '''
  58. 1. Quiz
  59. 2. Manage (Admin Only)
  60. 3. Quit'''
  61.  
  62. print(title)
  63. print(deco *len(title))
  64. print()
  65. print(menu)
  66.  
  67.  
  68. def login():
  69. with open("student_data.csv", "r") as file:
  70. read=csv.reader(file)
  71. invalid_login = True
  72. while invalid_login:
  73. username = input ('What is your username')
  74. password = input ('What is your password')
  75. for each in read:
  76. #print (each)
  77. if username in each and password in each:
  78. print ('Login Successful')
  79. invalid_login = False
  80. break
  81.  
  82. elif username not in each and password not in each:
  83. print('Incorrect username or password')
  84. loginmenu(username)
  85.  
  86. def get_validate_userdata():
  87. missing_data = True
  88. while missing_data:
  89. name = input ('Enter your name: ')
  90. surname = input ('Enter your surname: ')
  91. age = input ('Enter your age')
  92. group = input ('Enter your year group: ')
  93. password = password_check()
  94. if name == '' or surname == '' or group == '' or password == '' or age == '':
  95. print ('Fields cannot be blank')
  96. else:
  97. missing_data = False
  98. username =str(randint(10,99))+name[0:2]+surname[0:2]
  99. return (username, name, surname, group, age, password)
  100. break
  101.  
  102. def password_check():
  103. incorrect_data = True
  104. while incorrect_data:
  105. password = input ('Enter your password: ')
  106. for each in password:
  107. if each in string.ascii_uppercase:
  108. print('')
  109. incorrect_data = False
  110. break
  111. else:
  112. print ('Weak password, Your password must contain a capital letter')
  113. return password
  114.  
  115. def write_user_data():
  116. data = get_validate_userdata()
  117. data_to_write = []
  118. for each in data:
  119. data_to_write.append(each)
  120. print (data_to_write)
  121. with open ('student_records.csv','a') as studentFile:
  122. studentFileWriter = csv.writer(studentFile)
  123. studentFileWriter.writerow(data_to_write)
  124. print('Data written successfully')
  125. studentFile.close()
  126. main_menu()
  127.  
  128. main_menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement