Python help needed
Please help me with the Python project.
Some of the code is already done.
Description:
This program creates a class registration system. It allows
students to log in to add courses, drop courses and list courses
they have registered for.
This program has 5 functions in 2 modules / files: a student module
and a main module.
| Functions | Specification |
|---|---|
|
add_course(id, c_list, r_list, m_list) |
four parameters: id is the ID of the student to be added; |
| drop_course(id, c_list, r_list) | This function drops a student from a course. It has three parameters: id is the ID of the student to be dropped; c_list is the list of courses offered; r_list is the list of class rosters. This function asks user to enter the course he/she wants to drop. If the course is not offered, display error message and stop. If the student is not enrolled in that course, display error message and stop. Remove student ID from the course’s roster and display a message if there is no problem. This function has no return value. |
| list_courses(id, c_list, r_list) | This function displays and counts courses a student has
registered for. It has three parameters: id is the ID of the student; c_list is the list of courses offered; r_list is the list of class rosters. This function displays the number of courses the student has registered for and which courses they are. This function has no return value. |
# student.py file
def list_courses(id, c_list, r_list):
# ------------------------------------------------------------
# This function displays and counts courses a student has
# registered for. It has three parameters: id is the ID of the
# student; c_list is the course list; r_list is the list of
# class rosters. This function has no return value.
# -------------------------------------------------------------
def add_course(id, c_list, r_list, m_list):
# ------------------------------------------------------------
# This function adds a student to a course. It has four
# parameters: id is the ID of the student to be added; c_list
# is the course list; r_list is the list of class rosters;
# m_list is the list of maximum class sizes. This function
# asks user to enter the course he/she wants to add. If the
# course is not offered, display error message and stop.
# If the course is full, display error message and stop.
# If student has already registered for this course, display
# error message and stop. Add student ID to the course’s
# roster and display a message if there is no problem. This
# function has no return value.
# -------------------------------------------------------------
course = input("Enter course you want to add: ") # read input from user
if course in course_list:
index = course_list.index(course) # check for course in list
flag1 = 0
flag2 = 0
if len(r_list[index]) == max_size_list[index]: # check for seats availability
print("Course already full") # print message if seats are full
print()
flag1 = 1 # set2 flag
elif id in r_list[index] and flag1 != 1: # Check if student is already enrolled:
print("You are already enrolled in that course.")
flag2 = 1 # set flag2
print()
if flag1 != 1 and flag2 != 0:
r_list[index].append(id) # Append or add id to list if student is no registered
else:
print("Course not found") # print message if user enters invalid course name
print()
return flag1, flag2
def drop_course(id, c_list, r_list):
# ------------------------------------------------------------
# This function drops a student from a course. It has three
# parameters: id is the ID of the student to be dropped;
# c_list is the course list; r_list is the list of class
# rosters. This function asks user to enter the course he/she
# wants to drop. If the course is not offered, display error
# message and stop. If the student is not enrolled in that
# course, display error message and stop. Remove student ID
# from the course’s roster and display a message if there is
# no problem. This function has no return value.
# -------------------------------------------------------------
course = input("Enter course you want to drop: ") # Read input from user
if course in course_list:
index = course_list.index(course) # check for course in list
if id in r_list[index]:
r_list[index].remove(id) # remove the student from that course
print("Course dropped") # print message
print()
else:
print("You are not registered for this course. ") # print if student is not registered for that course
print()
else:
print("Course not found") # print if invalid course is entered by user.
print()
# registration.py
| Functions | Specification |
|---|---|
| login(id, s_list) | This function allows a student to log in. It has two
parameters: id and s_list, which is the student list. This function asks user to enter PIN. If the ID and PIN combination is in s_list, display message of verification and return True. Otherwise, display error message and return False. |
| main() |
This function manages the whole registration system. It has no
parameter. drop courses or list courses the student has registered for. This function has no return value. |
# registration.py
import student
import sys
def main():
# ------------------------------------------------------------
# This function manages the whole registration system. It has
# no parameter. It creates student list, course list,
# max class size list and roster list. It uses a loop to serve
# multiple students. Inside the loop, ask student to enter ID,
# and call the login function to verify student's identity. Then
# let student choose to add course, drop course or list courses.
# This function has no return value.
# -------------------------------------------------------------
student_list = [('1001', '111'), ('1002', '222'), ('1003', '333'), ('1004', '444')]
course_list = ['CSC101', 'CSC102', 'CSC103']
roster_list = [['1004', '1003'], ['1001'], ['1002']]
max_size_list = [3, 2, 1]
if id == 0:
sys.exit()
else:
login(id, student_list)
return student_list, course_list, roster_list, max_size_list
def login(id, s_list):
# ------------------------------------------------------------
# This function allows a student to log in.
# It has two parameters: id and s_list, which is the student
# list. This function asks user to enter PIN. If the ID and PIN
# combination is in s_list, display message of verification and
# return True. Otherwise, display error message and return False.
# -------------------------------------------------------------
# pass temporarily avoid empty function definition
while True:
pin = input("Enter PIN: ") # Read pin from user
flag = 0
for i in student_list:
if pin == i[1]: # validate user id and pin
print("ID and PIN verified")
break
else:
print("ID or PIN incorrect")
flag = 1 # set flag in invalid
break
print()
if flag == 1: # If credentials entered are not valid, prompt and re enter them.
continue
ch = -1
print()
while ch != 0: # If valid credentials, then display menu
ch = int(input("Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to exit: "))
if ch == 0:
print("Student session ended") # exit if 0 is selected
return
elif ch == 1:
add_course(id, roster_list, max_size_list) # add course if 1 is selected.
elif ch == 2:
drop_course(id, roster_list) # drop course if 2 is selected
elif ch == 3:
list_courses(id, roster_list) # display courses registered if 3 is selected.
main()
# Output Example
Enter ID to log in, or 0 to quit: 1234
Enter PIN: 123
ID or PIN incorrect
Enter ID to log in, or 0 to quit: 1001
Enter PIN: 111
ID and PIN verified
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 1
Enter course you want to add: CSC121
Course not found
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 1
Enter course you want to add: CSC102
You are already enrolled in that course.
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 1
Enter course you want to add: CSC103
Course already full.
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 1
Enter course you want to add: CSC101
Course added
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 2
Enter course you want to drop: CSC121
Course not found
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 2
Enter course you want to drop: CSC103
You are not enrolled in that course.
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 2
Enter course you want to drop: CSC102
Course dropped
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 3
Courses registered:
CSC101
Total number: 1
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 1
Enter course you want to add: CSC102
Course added
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 3
Courses registered:
CSC101
CSC102
Total number: 2
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 0
Session ended.
Enter ID to log in, or 0 to quit: 1002
Enter PIN: 222
ID and PIN verified
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 3
Courses registered:
CSC103
Total number: 1
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 1
Enter course you want to add: CSC101
Course already full.
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 1
Enter course you want to add: CSC102
Course added
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 3
Courses registered:
CSC102
CSC103
Total number: 2
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 0
Session ended.
Enter ID to log in, or 0 to quit: 0
Please find the code below: Also adding the sample output.
Code for student.py
-----------------------------------------------------------------------------------------
def add_course(id, c_list, r_list, m_list):
# ------------------------------------------------------------
# This function adds a student to a course. It has four
# parameters: id is the ID of the student to be added; c_list
# is the course list; r_list is the list of class rosters;
# m_list is the list of maximum class sizes. This function
# asks user to enter the course he/she wants to add. If the
# course is not offered, display error message and stop.
# If the course is full, display error message and stop.
# If student has already registered for this course, display
# error message and stop. Add student ID to the course’s
# roster and display a message if there is no problem. This
# function has no return value.
# -------------------------------------------------------------
course_num = 0
course = input("Enter course you want to add: ")
if course not in c_list: #Check if entered course is in course list
print("Course not found")
return
else:
index = c_list.index(course) # find the position of course in list
if len(r_list[index]) == m_list[index]: # check for seats availability
print("Course already full.") # print message if seats are full
return
if id in r_list[index]: # Check if student is already enrolled:
print("You are already enrolled in that course.") #print message if student is already enrolled
return
else:
r_list[index].append(id) #enroll the student for the course
print("Course added") #Print message
def drop_course(id, c_list, r_list):
# ------------------------------------------------------------
# This function drops a student from a course. It has three
# parameters: id is the ID of the student to be dropped;
# c_list is the course list; r_list is the list of class
# rosters. This function asks user to enter the course he/she
# wants to drop. If the course is not offered, display error
# message and stop. If the student is not enrolled in that
# course, display error message and stop. Remove student ID
# from the course’s roster and display a message if there is
# no problem. This function has no return value.
# -------------------------------------------------------------
course = input("Enter course you want to drop: ") # Read course name from user
if course not in c_list:
print("Course not found") #Print message
return
else:
index = c_list.index(course)
if id not in r_list[index]:
print("You are not enrolled in that course.") #Print message if id is not in roaster list
return
else:
r_list[index].remove(id) #Remove the student id from the roaster for the course
print("Course dropped") #Print message
def list_courses(id, c_list, r_list):
# ------------------------------------------------------------
# This function displays and counts courses a student has
# registered for. It has three parameters: id is the ID of the
# student; c_list is the course list; r_list is the list of
# class rosters. This function has no return value.
# -------------------------------------------------------------
print("Courses registered:")
total = 0
l = len(c_list)
for i in range(0, len(c_list)):
if id in r_list[i]:
print(c_list[i])
total= total+1
print("Total number: ",total)
-------------------------------------------------------------------------------------------------------------------
registration.py
import student # Import functions from stident.py
import sys
def main():
# ------------------------------------------------------------
# This function manages the whole registration system. It has
# no parameter. It creates student list, course list,
# max class size list and roster list. It uses a loop to serve
# multiple students. Inside the loop, ask student to enter ID,
# and call the login function to verify student's identity. Then
# let student choose to add course, drop course or list courses.
# This function has no return value.
# -------------------------------------------------------------
student_list = [('1001', '111'), ('1002', '222'), ('1003', '333'), ('1004', '444')]
course_list = ['CSC101', 'CSC102', 'CSC103']
roster_list = [['1004', '1003'], ['1001'], ['1002']]
max_size_list = [3, 2, 1]
while True:
id = input("\nEnter ID to log in, or 0 to quit: ")
if id == '0':
break
else:
result = login(id,student_list)
if result == True:
while True:
student_action = (input("\nEnter 1 to add course, 2 to drop course, 3 to list courses, 0 to exit: "))
if student_action == "1":
student.add_course(id,course_list,roster_list,max_size_list)
elif student_action == "2":
student.drop_course(id,course_list,roster_list)
elif student_action == "3":
student.list_courses(id,course_list,roster_list)
elif student_action == "0":
print("Session ended")
break
else:
print("Invalid Input....Please try again")
def login(id,s_list):
# ------------------------------------------------------------
# This function allows a student to log in.
# It has two parameters: id and s_list, which is the student
# list. This function asks user to enter PIN. If the ID and PIN
# combination is in s_list, display message of verification and
# return True. Otherwise, display error message and return False.
# -------------------------------------------------------------
pin = (input("Enter PIN: ")) # Read pin from user
for i in s_list:
if i[0] == id: #validate user id
if i[1] == pin: #validate pin
print("ID and PIN verified")
return True
else:
print("ID or PIN incorrect")
return False
print("ID or PIN incorrect")
return False
main()
-------------------------------------------------------------------------------------------------------------------
Sample Output
-----------------------------------
Enter ID to log in, or 0 to quit: 1234
Enter PIN: 123
ID or PIN incorrect
Enter ID to log in, or 0 to quit: 1001
Enter PIN: 111
ID and PIN verified
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 1
Enter course you want to add: CSC121
Course not found
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 1
Enter course you want to add: CSC102
You are already enrolled in that course.
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 1
Enter course you want to add: CSC103
Course already full.
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 1
Enter course you want to add: CSC101
Course added
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 2
Enter course you want to drop: CSC121
Course not found
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 2
Enter course you want to drop: CSC103
You are not enrolled in that course.
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 2
Enter course you want to drop: CSC102
Course dropped
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 3
Courses registered:
CSC101
Total number: 1
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 1
Enter course you want to add: CSC102
Course added
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 3
Courses registered:
CSC101
CSC102
Total number: 2
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 0
Session ended
Enter ID to log in, or 0 to quit: 1002
Enter PIN: 222
ID and PIN verified
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 3
Courses registered:
CSC103
Total number: 1
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 1
Enter course you want to add: CSC101
Course already full.
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 1
Enter course you want to add: CSC102
Course added
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 3
Courses registered:
CSC102
CSC103
Total number: 2
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 0
Session ended
Enter ID to log in, or 0 to quit: 0
>>>
Sample Output2:
Enter ID to log in, or 0 to quit: 1002
Enter PIN: 222
ID and PIN verified
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 1
Enter course you want to add: CS101
Course not found
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 1
Enter course you want to add: CSC101
Course added
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 0
Session ended
Enter ID to log in, or 0 to quit: 1003
Enter PIN: 333
ID and PIN verified
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to
exit: 0
Session ended
Enter ID to log in, or 0 to quit: 1004
Enter PIN: 444
ID and PIN verified
Enter 1 to add course, 2 to drop course, 3 to list courses, 0 to exit:
Python help needed Please help me with the Python project. Some of the code is already...