Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. import sys
  2. import collections
  3. import os
  4.  
  5. try:
  6. create = open('studentlist.txt', 'x')
  7. create.close()
  8.  
  9. except:
  10. print('Student file already exists')
  11.  
  12. def optionHandler(option):
  13. if option == 'a':
  14. addStudent()
  15. elif option == 'd':
  16. deleteStudent()
  17. elif option == 'r':
  18. readList()
  19. elif option == 'q':
  20.  
  21. sys.exit()
  22. else:
  23. return -1
  24.  
  25.  
  26. def addStudent():
  27. studentDict = {}
  28. ID = input('Student ID number? ')
  29. try:
  30. ID = int(ID)
  31. except:
  32. return print('ID not a number')
  33. fName = input('Student first name? ')
  34. lName = input('Student last name? ')
  35. major = input('Student major? ')
  36. studentfile = open('studentlist.txt', 'r')
  37. line = studentfile.readline()
  38. while line:
  39. curStudent = line.split(',')
  40. curStudent = list(map(str.strip, curStudent))
  41. curStudentIDNum = int(curStudent[0])
  42. curStudentDict = {'fName': curStudent[1], 'lName' : curStudent[2], 'Major' : curStudent[3]}
  43. studentDict[curStudentIDNum] = {'fName': curStudent[1], 'lName' : curStudent[2], 'Major' : curStudent[3]}
  44. line = studentfile.readline()
  45. studentDict[ID] = {'fName': fName, 'lName' : lName, 'Major' : major}
  46. studentfile.close()
  47. newfile = open('studentlist.txt', 'w').close()
  48. newfile = open('studentlist.txt', 'w')
  49. studentDict = collections.OrderedDict(sorted(studentDict.items(), key=lambda x: int(x[0])))
  50. for ID in studentDict:
  51. newfile.write(str(ID)+','+studentDict[ID]['fName']+','+studentDict[ID]['lName']+','+studentDict[ID]['Major']+'\n')
  52. newfile.close()
  53.  
  54.  
  55. def deleteStudent():
  56. studentDict = {}
  57. studentfile = open('studentlist.txt', 'r')
  58. line = studentfile.readline()
  59. while line:
  60. curStudent = line.split(',')
  61. curStudent = list(map(str.strip, curStudent))
  62. curStudentIDNum = int(curStudent[0])
  63. curStudentDict = {'fName': curStudent[1], 'lName' : curStudent[2], 'Major' : curStudent[3]}
  64. studentDict[curStudentIDNum] = {'fName': curStudent[1], 'lName' : curStudent[2], 'Major' : curStudent[3]}
  65. line = studentfile.readline()
  66. studentID = input('Student ID to remove? ')
  67. try:
  68. studentID = int(studentID)
  69. except:
  70. print('Invalid ID: not a number')
  71.  
  72. try:
  73. del studentDict[studentID]
  74. except:
  75. print('Invalid ID: key not found')
  76. newfile = open('studentlist.txt', 'w').close()
  77. newfile = open('studentlist.txt', 'w')
  78. studentDict = collections.OrderedDict(sorted(studentDict.items(), key=lambda x: int(x[0])))
  79. for ID in studentDict:
  80. newfile.write(str(ID)+','+studentDict[ID]['fName']+','+studentDict[ID]['lName']+','+studentDict[ID]['Major']+'\n')
  81. newfile.close()
  82.  
  83.  
  84. def readList():
  85. studentDict= {}
  86. studentfile = open('studentlist.txt', 'r')
  87. line = studentfile.readline()
  88. while line:
  89. curStudent = line.split(',')
  90. curStudent = list(map(str.strip, curStudent))
  91. curStudentIDNum = int(curStudent[0])
  92. curStudentDict = {'fName': curStudent[1], 'lName' : curStudent[2], 'Major' : curStudent[3]}
  93. studentDict[curStudentIDNum] = {'fName': curStudent[1], 'lName' : curStudent[2], 'Major' : curStudent[3]}
  94. line = studentfile.readline()
  95. print("{:<9} {:<10} {:<10} {:<10}"
  96. .format('ID', 'First Name', 'Last Name', 'Major'))
  97. for ID in studentDict:
  98. print("{:<10} {:<10} {:<10} {:<10}"
  99. .format(str(ID),studentDict[ID]['fName'],studentDict[ID]['lName'],studentDict[ID]['Major']))
  100. while(True):
  101. option = input('Pick an option:\nAdd a student-(a)\nDelete a student-(d)\nRead the student list-(r)\nQuit-(q)\nInput: ')
  102. if optionHandler(option) == -1:
  103. print('Invalid Option')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement