From ba9a896514d6f4d59b4f40caf316ef8e5e60681c Mon Sep 17 00:00:00 2001 From: Sumanth Date: Sun, 19 May 2024 19:55:37 -0400 Subject: [PATCH] Made changes to Options and input check --- menu-based_student_record.py | 155 ++++++++++++++++++++++++----------- 1 file changed, 109 insertions(+), 46 deletions(-) diff --git a/menu-based_student_record.py b/menu-based_student_record.py index 2d892f77..557d4a98 100644 --- a/menu-based_student_record.py +++ b/menu-based_student_record.py @@ -1,68 +1,131 @@ -singlerecord=[] -studList=[] -choice='y' -while choice.lower()!='n': - studName=input('enter your name ') - Rollno=input('enter your Rollno ') - cgpa=int(input('cgpa :')) - singlerecord.append(studName) - singlerecord.append(Rollno) - singlerecord.append(cgpa) - studList.append(singlerecord) - singlerecord=[] - choice=input('enter your choice [y/n]') +studList = [] -#append def appendStudent(): global studList singlr = [] - studName=input('enter your name') - Rollno=input('enter your Rollno') - cgpa=int(input('cgpa :')) + studName = input('Enter your name: ') + + while True: + Rollno = input('Enter your Rollno: ') + if Rollno.isdigit(): + Rollno = int(Rollno) + break + else: + print("Rollno should be an integer. Please try again.") + + while True: + try: + cgpa = float(input('CGPA (decimal): ')) + break + except ValueError: + print("CGPA should be a decimal number. Please try again.") + singlr.append(studName) singlr.append(Rollno) singlr.append(cgpa) studList.append(singlr) - - -# remove existing record by rollno + +# Remove existing record by rollno def removeRecord(): global studList - rolln=int(input("enter student's rollno:")) + while True: + rolln = input("Enter student's Rollno: ") + if rolln.isdigit(): + rolln = int(rolln) + break + else: + print("Rollno should be an integer. Please try again.") + + found = False for record in studList: - if rolln in record: + if rolln == record[1]: # Comparing Rollno which is the second item in the list studList.remove(record) + found = True + break # Exit the loop once the record is found and removed + + if not found: + print("Student not found.") -# view student by name: + +# View student by name def viewRecord(): - name=input("enter student's name:") + name = input("Enter student's name: ") + found = False for record in studList: - if name in record: + if name == record[0]: # Comparing name which is the first item in the list print(record) + found = True + break + if not found: + print("Student not found.") + -# copy +# Copy list (functionality can be implemented as needed) def copyList(): - print("what to do??") + global studList + copiedList = studList.copy() + print("Copied list:", copiedList) -#remove all +# View all students +def viewAllRecords(): + if not studList: + print("No students in the list.") + else: + for record in studList: + print(record) + +# Remove all records def removeAll(): + global studList studList.clear() + print("All records removed.") + +# Show menu +def showMenu(): + print("Enter:") + print("Option 1: Append student") + print("Option 2: Remove student by Rollno") + print("Option 3: View student by name") + print("Option 4: Copy list") + print("Option 5: View all Students ") + print("Option 6: Remove all students") + print("Option 7: Exit") + +# Main program loop +def main(): + flag = 'y' + while flag.lower() == 'y': + showMenu() + try: + find = int(input("Enter your choice: ")) + if find == 1: + appendStudent() + elif find == 2: + removeRecord() + elif find == 3: + viewRecord() + elif find == 4: + copyList() + elif find == 5: + viewAllRecords() + elif find == 6: + removeAll() + elif find == 7: + print("Exiting the program.") + break + else: + print("Enter correct option !!") + except ValueError: + print("Please enter a valid number.") + + while True: + flag = input("Do you want to continue? (y/n): ").lower() + if flag in ['y', 'n']: + break + else: + print("Invalid input. Please enter 'y' or 'n'.") + +# When the code is run, the main function will be executed first to give options to the user. +if __name__ == "__main__": + main() -flag = 'y' -while (flag == 'y'): - find = int(input("Enter \n option 1 : Append student \n option 2: remove student by Rollno\n option 3: view student by name\n option 4: copy list\n option 5: remove all students option \n 6: exit")) - if (find == 1): - appendStudent() - elif (find == 2): - removeRecord() - elif (find == 3): - viewRecord() - elif (find == 4): - copyList() - elif (find == 5): - removeAll() - elif (find == 6): - exit - else: - print("enter correct option !!") - flag = input("do u want to continue ? say y or n : \n")