Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import collections
- import os
- try:
- create = open('studentlist.txt', 'x')
- create.close()
- except:
- print('Student file already exists')
- def optionHandler(option):
- if option == 'a':
- addStudent()
- elif option == 'd':
- deleteStudent()
- elif option == 'r':
- readList()
- elif option == 'q':
- sys.exit()
- else:
- return -1
- def addStudent():
- studentDict = {}
- ID = input('Student ID number? ')
- try:
- ID = int(ID)
- except:
- return print('ID not a number')
- fName = input('Student first name? ')
- lName = input('Student last name? ')
- major = input('Student major? ')
- studentfile = open('studentlist.txt', 'r')
- line = studentfile.readline()
- while line:
- curStudent = line.split(',')
- curStudent = list(map(str.strip, curStudent))
- curStudentIDNum = int(curStudent[0])
- curStudentDict = {'fName': curStudent[1], 'lName' : curStudent[2], 'Major' : curStudent[3]}
- studentDict[curStudentIDNum] = {'fName': curStudent[1], 'lName' : curStudent[2], 'Major' : curStudent[3]}
- line = studentfile.readline()
- studentDict[ID] = {'fName': fName, 'lName' : lName, 'Major' : major}
- studentfile.close()
- newfile = open('studentlist.txt', 'w').close()
- newfile = open('studentlist.txt', 'w')
- studentDict = collections.OrderedDict(sorted(studentDict.items(), key=lambda x: int(x[0])))
- for ID in studentDict:
- newfile.write(str(ID)+','+studentDict[ID]['fName']+','+studentDict[ID]['lName']+','+studentDict[ID]['Major']+'\n')
- newfile.close()
- def deleteStudent():
- studentDict = {}
- studentfile = open('studentlist.txt', 'r')
- line = studentfile.readline()
- while line:
- curStudent = line.split(',')
- curStudent = list(map(str.strip, curStudent))
- curStudentIDNum = int(curStudent[0])
- curStudentDict = {'fName': curStudent[1], 'lName' : curStudent[2], 'Major' : curStudent[3]}
- studentDict[curStudentIDNum] = {'fName': curStudent[1], 'lName' : curStudent[2], 'Major' : curStudent[3]}
- line = studentfile.readline()
- studentID = input('Student ID to remove? ')
- try:
- studentID = int(studentID)
- except:
- print('Invalid ID: not a number')
- try:
- del studentDict[studentID]
- except:
- print('Invalid ID: key not found')
- newfile = open('studentlist.txt', 'w').close()
- newfile = open('studentlist.txt', 'w')
- studentDict = collections.OrderedDict(sorted(studentDict.items(), key=lambda x: int(x[0])))
- for ID in studentDict:
- newfile.write(str(ID)+','+studentDict[ID]['fName']+','+studentDict[ID]['lName']+','+studentDict[ID]['Major']+'\n')
- newfile.close()
- def readList():
- studentDict= {}
- studentfile = open('studentlist.txt', 'r')
- line = studentfile.readline()
- while line:
- curStudent = line.split(',')
- curStudent = list(map(str.strip, curStudent))
- curStudentIDNum = int(curStudent[0])
- curStudentDict = {'fName': curStudent[1], 'lName' : curStudent[2], 'Major' : curStudent[3]}
- studentDict[curStudentIDNum] = {'fName': curStudent[1], 'lName' : curStudent[2], 'Major' : curStudent[3]}
- line = studentfile.readline()
- print("{:<9} {:<10} {:<10} {:<10}"
- .format('ID', 'First Name', 'Last Name', 'Major'))
- for ID in studentDict:
- print("{:<10} {:<10} {:<10} {:<10}"
- .format(str(ID),studentDict[ID]['fName'],studentDict[ID]['lName'],studentDict[ID]['Major']))
- while(True):
- option = input('Pick an option:\nAdd a student-(a)\nDelete a student-(d)\nRead the student list-(r)\nQuit-(q)\nInput: ')
- if optionHandler(option) == -1:
- print('Invalid Option')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement