Advertisement
Kulas_Code20

Student Menu in Python

Sep 17th, 2022 (edited)
1,297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.19 KB | Source Code | 0 0
  1. """
  2. A program that would accept a set dictionary data
  3. student: dict={
  4.    'idno':<inputted data>
  5.    'name':<inputted data>
  6.    'course':<inputted data>
  7.    'level':<inputted data>
  8. }
  9. and put it in a list, create a menu that would
  10. facilitate the operators on the options provided
  11. Details below:
  12. ------Main Menu------
  13. 1. Add Student
  14. 2. Find Student using idno
  15. 3. Delete Student using idno
  16. 4. Display all
  17. 0. Quit/End
  18. ---------------------
  19. Enter Option(0..4):
  20. """
  21. from os import system
  22.  
  23. student_list: list = []
  24.  
  25. def getdata() -> dict:
  26.     system("cls")
  27.     student: dict = {}  # empty dictionary, not an empty set
  28.     try:
  29.         idno: int = int(input("Enter id number: "))
  30.         student['idnum'] = idno
  31.     except Exception:
  32.         print("Invalid Age")
  33.         student.setdefault('idnum', 0)
  34.     studentname: str = input("Enter student name: ")
  35.     course: str = input("Enter course: ")
  36.     level: str = input("Enter level: ")
  37.  
  38.     student['studentname'] = studentname
  39.     student['course'] = course
  40.     student['level'] = level
  41.  
  42.     return student
  43.  
  44. def addstudent() -> None:
  45.     global student_list
  46.     stud: dict = getdata()
  47.     student_list.append(stud)
  48.  
  49. def findstudent() -> None:
  50.     idno: int = int(input("Enter id number: "))
  51.     if len(student_list) > 0:
  52.         for student in student_list:
  53.             if student['idnum'] == idno:
  54.                 print(student['idnum'], end=' ')
  55.                 print(student['studentname'], end=' ')
  56.                 print(student['course'], end=' ')
  57.                 print(student['level'], end=' ')
  58.                 print("")
  59.                 break
  60.     else:
  61.         print("List is Empty")
  62.     input("Press any key to continue...")
  63.  
  64. def deletestudent() -> None:
  65.     print("----Delete a student from the list----")
  66.     idno: int = int(input("Enter id number: "))
  67.     if len(student_list) > 0:
  68.         for student in student_list:
  69.             if student['idnum'] == idno:
  70.                 student_list.remove(student)
  71.                 break
  72.     else:
  73.         print('Student did not exist!')
  74.     input("Press any key to continue...")
  75.  
  76.  
  77. def displayall() -> None:
  78.     if len(student_list) > 0:
  79.         for student in student_list:
  80.             print(f"{student['idnum']} {student['studentname']} {student['course']} {student['level']}")
  81.     else:
  82.         print("List is empty !")
  83.  
  84.     input("Press any key to continue")
  85.  
  86. def terminate() -> None:
  87.     print("Program Ends...")
  88.  
  89. def menuselect(option: int) -> None:
  90.     mymenu = {
  91.         1: addstudent,
  92.         2: findstudent,
  93.         3: deletestudent,
  94.         4: displayall,
  95.         0: terminate
  96.     }
  97.     func = mymenu.get(option)
  98.     return func()
  99.  
  100.  
  101. def menu() -> None:
  102.     system("cls")
  103.     menuitems = (
  104.         '-----Main Menu----',
  105.         '1. Add Student',
  106.         '2. Find Student using idno',
  107.         '3. Delete Student using idno',
  108.         '4. Display All',
  109.         '0. Quit/End ',
  110.         '------------------'
  111.     )
  112.     [print(menuitem) for menuitem in menuitems]
  113.  
  114. def main() -> None:
  115.     opt: int = -1
  116.     while opt != 0:
  117.         menu()
  118.         opt = int(input("Enter Option (0..4):"))
  119.         menuselect(opt)
  120.  
  121. if __name__ == "__main__":
  122.     main()
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement