PLEASE SERIOUS PROGRAMER ONLY!. I ALWAYS RATE MY ANSWERS. Write a program that u
ID: 3794667 • Letter: P
Question
PLEASE SERIOUS PROGRAMER ONLY!. I ALWAYS RATE MY ANSWERS.
Write a program that uses Python’s Dictionaries to accomplish the following. Be sure to MODULARIZE your program, and pass everything that a function needs in as parameters.
Create an empty dictionary called stuRoster.
Use these functions that were created previously:
createMenu
getValidChoice
Create this function:
isValidPhone(tmpPhone) which accepts any string and returns True if the string is in one of these formats: ###-###-#### OR (###)###-#### . Otherwise, it returns False.
Continuously print the menu shown below until the user chooses the option to quit.
Be sure to use the getValidChoice function to ensure the choice is numeric and within the range allowed.
Add Student
Print Report (All Students)
Print Info for Individual Student
Calculate Average GPA of all students
Print Count for each Major
Delete Student
Quit
Create functions for each of the menu options – be sure to pass in the dictionary as a parameter!
Add Student option: (write a function to handle this – make sure to pass enough parameters into this!)
Use Python input statements to have the user enter the following information: studentID, lastName, firstName, phoneNumber, major, GPA
Make sure to call the isValidPhone function to ensure they enter a valid phone number.
Check to ensure that the studentID isn’t a duplicate! If it is, keep asking the user to enter a different ID until a unique one is entered.NO DUPLICATES ALLOWED!
Store the information in the stuRoster dictionary – it should map ids to a list containing lastName, firstName, phoneNumber, major, GPA.
Example of how the stuRoster will look after 2 calls to the “addStudent” function:
{ “J12”: [“Jones”,”Jill”,”111-222-3333”,”Math”,3.5],
“S45”: [“Smith”,”Sam”,”(218)333-4444”,”Sociology”,4.0],
“B23”:[“Jones”, “Jonathon”, “555-444-3333”, “Math”, 3.2]
}
Print Report option: (write a function to handle this– make sure to pass enough parameters into this)
Print the Column Headings
Print the information for each student on a separate line.
NOTE: use Python’s format method to ensure that the information is aligned in columns.
Example:
ID First Last Phone Major GPA
Name Name Number
----------------------------------------------------------------------------------------------------------------------------
J12 Jill Jones 111-222-3333 Math 3.5
S45 Sam Smith (218)333-4444 Sociology 4.0
B23 Jonathon Jones 555-444-3333 Math 3.2
Print Info for Individual option: (write a function to handle this)
Ask the user to enter the last name.
Look to see if anyone with that last name is in the dictionary. If so, print the information for that student.
If there is more than one student with that last name, print information for all .
If that last name doesn’t appear in the dictionary, print a message “That student is not in the roster”.
Example:
Enter the last name of the student: Smith
Name: Sam Smith
ID: S45
Phone: (218)333-4444
Major: Sociology
GPA: 4.0
Example:
Enter the last name of the student: Brown
That student is not in the roster.
Example:
Enter the last name of the student: Jones
Name: Jill Jones
ID: J12
Phone: 111-222-3333
Major: Math
GPA: 3.5
Name: Jonathon Jones
ID: B23
Phone: 555-444-3333
Major: Math
GPA: 3.2
Calculate Average GPA of all students: (define a function to handle this- pass all information needed as parameters)
Calculate the average and print it, rounding to the nearest tenth.
Print Count for Each Major option: (define a function to handle this)
Example: if stuRoster looks like this: { “B12”:[“Brown”,”Bill”,”Biology”,4.0],
“A61”:[“Adleson”,”Allen”,”Anatomy”,3.5],
“C24”:[ “Caulfield”,”Calvin”,”Chemistry”,2.5 ]
“J67”:[ “Jones”,”Jim”,”Anatomy”,3.8] }
the output would be similar to this (order is unpredictable though):
Anatomy 2
Chemistry 1
Biology 1
Delete Student option: (write a function to handle this)
Print a mini-menu that lists all of the ids that are in the dictionary and ask the user to select one of those.
Delete the entry with that ID from the dictionary and print a message “Successfully Deleted”.
Scoring Guide: 20 pts
No Documentation, No Credit
Program must be modularized – use functions with parameters. NO GLOBAL VARIABLES ALLOWED – pass the dictionary to each function!
2 pts prints menu continuously until user chooses quit
2 pts isValidPhone function
2 pts createMenu function
2 pts getValidChoice function doeserror-checking on user’s menu choice, ensuring only digits were entered in the appropriate range.
2 pts Add – checks to ensure no duplicate IDs are put into the dictionary
2 pts Print Report –prints column headings; uses format method to ensure alignment of columns
2 pts Print Individual Info – if more than one, prints all with that last name; If not there, prints an appropriate message.
2 pts Calculates average GPA correclty, prints to nearest tenth
2 pts Prints counts for each major output is aligned in columns
2 pts Prints a mini-menu displaying all ids in the dictionary and lets user check one. Deletes entry for student.
Explanation / Answer
# this functions populates the menu from list ofmenu items
def createMenu(menu):
print ' Please enter your choice : '
count=1;
for menuItem in menu:
print str(count)+' : '+menuItem
count +=1
print " "
# GetValidChoice() returns true if choice is in range specifed as options
def getValidChoice():
try:
inputChoice=int(raw_input())
if(inputChoice > 7 or inputChoice < 1):
raise
except:
'Enter a valid choice '
return getValidChoice()
return inputChoice
# returns true for phone numbers of format ###-###-#### or (###)###-####.
def isValidPhone(tmpPhone):
import re
r1 = re.compile('[0-9]{3,3}[-][0-9]{3,3}[-][0-9]{4,4}')
r2 = re.compile('[(][0-9]{3,3}[)][0-9]{3,3}[-][0-9]{4,4}')
if (r1.match(tmpPhone) is not None) or (r2.match(tmpPhone) is not None):
return True
return False
# logic to add student to the dictionary stuRoster
def addStudent(stuRoster):
details=[]
studentID = raw_input(' Enter Student ID : ')
while(studentID in stuRoster.keys()): # check if the studentID already exists in dictionary
print 'Enter different student ID: '
studentID = raw_input('Enter Student ID: ')
lastName = raw_input('Enter last name: ')
firstName = raw_input('Enter first name: ')
phoneNumber = raw_input('Enter phone number: ')
while(isValidPhone(phoneNumber)!=True): # check if phone number is in valid format
print 'Enter phone number in "###-###-####" or "(###)###-####" format'
phoneNumber = raw_input('Enter phone number: ')
major = raw_input('Enter major: ')
GPA = raw_input('Enter GPA: ')
details.append(lastName)
details.append(firstName)
details.append(phoneNumber)
details.append(major)
details.append(GPA)
stuRoster[studentID]=details #add record to dictionary
# Function to print the full report in formatted way
def printFullReport(stuRoster):
print " ID First Last Phone Major GPA".format()
for key, value in stuRoster.iteritems():
studentID = key
lastName = value[0]
firstName = value[1]
phoneNumber = value[2]
major = value[3]
GPA = value[4]
print (""+studentID+" "+firstName+" "+lastName+" "+phoneNumber+" "+major+" "+GPA).format()
#function to handle request to print only specific students report
def printStudentReport(stuRoster):
print ' Enter the last name of student'
lname=raw_input()
found=False;
print " "
for key, value in stuRoster.iteritems():
if(value[0] == lname):
found=True
studentID = key
lastName = value[0]
firstName = value[1]
phoneNumber = value[2]
major = value[3]
GPA = value[4]
print " "
print "Name: "+firstName + " "+lastName
print "ID: "+studentID
print "Phone: "+phoneNumber
print "Major: "+major
print "GPA: "+GPA
if(found != True):
print 'That student is not in the roster'
# function to print average of GPA per major
def calcAverageGPA(stuRoster):
sumPerMajor={}
studentsPerMajor={}
for key, value in stuRoster.iteritems():
if(value[3] not in sumPerMajor.keys()):
sumPerMajor[value[3]]=float(value[4])
studentsPerMajor[value[3]]=1
else:
sumPerMajor[value[3]] += float(value[4])
studentsPerMajor[value[3]] += 1
for key,value in sumPerMajor.iteritems():
print key+": "+ str(value/studentsPerMajor[key])
# function to print counts of students per major
def printCountPerMajor(stuRoster):
studentsPerMajor={}
for key, value in stuRoster.iteritems():
if(value[3] not in studentsPerMajor.keys()):
studentsPerMajor[value[3]]=1
else:
studentsPerMajor[value[3]] += 1
for key,value in studentsPerMajor.iteritems():
print key+" "+str(value)
# function to delete the specified student record
def deleteStudent(stuRoster):
print ' Select the ID of student :'
for key in stuRoster.keys():
print key
print " "
id = raw_input()
if (stuRoster[id] is not None):
del stuRoster[id]
else:
print 'No such student with id :'+id
def main():
stuRoster={}
menu=['Add Student','Print Report (All Students)','Print Info for Individual Student','Calculate Average GPA of all students','Print Count for each Major','Delete Student','Quit']
choice=0;
createMenu(menu)
choice = getValidChoice()
while(choice != 7): # show choice until user selects to Quit( option = 7)
if(choice==1):
addStudent(stuRoster) # add student to the record
elif(choice==2):
printFullReport(stuRoster) # print full record
elif(choice==3):
printStudentReport(stuRoster) #print reord for a specific student ID
elif(choice==4):
calcAverageGPA(stuRoster) # print averageGPA per Major
elif(choice==5):
printCountPerMajor(stuRoster) # print student count per major
elif(choice==6):
deleteStudent(stuRoster) # delete specified student record
createMenu(menu) # recreate menu
choice=getValidChoice() # ask for next choice
return 0
if __name__ == "__main__": main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.