1

I wan to print Student data and Class data. So, I wrote a code to fetch Student_id and Class_id.

  • Firstly the Input Student_id must match with the Stud_id in Student_data.csv file and print the data according.[I found the answer for this]

  • Samely, the input class_id must match with the Class_id in Class_data.csv file and print the data according.

I need the Final output as below[marked in **]:

Option 8: Enrol a student to a class Enter student id: S001 Enter class id: C001

  • Student S001, Kitty Tan enrolled into class C001, CCW101, Computer and Information Processing *

Code:

if option == 8:

            Stud_id = str(input("Enter Student Id : ")).lower()
            Clas_id = str(input("Enter Class ID : ")).lower()

            Stud_File=open('Student_Data.csv', "r")                             ## Reading CSV file.
            Stud_Reader=list(csv.reader(Stud_File, delimiter=','))            ## Converting the csv file into List.
            Class_File=open('Class_Data.csv', "r")                             ## Reading CSV file.
            Class_Reader=list(csv.reader(Stud_File, delimiter=','))            ## Converting the csv file into List.

            for row in Stud_Reader:                                               ## Creating a for loop to search sub-string in a string in a CSV file data.
                    for field in row:                                
                            if Stud_id in field:                                        
                                    print("Student " +Stud_id), row[1],(" Enrolled into Class " +Clas_id)


            for row in Class_Reader:
                    for field in row:
                            if Clas_id in field:
                                    print(row[1], row[2]) 

Student S001, Kitty Tan enrolled into class C001, CCW101, Computer and Information Processing *

3
  • 1
    Can you describe what your code is doing incorrectly? Commented Sep 18, 2019 at 13:10
  • Class_Reader=list(csv.reader(Stud_File, delimiter=',')) should probably be reading from the Class_File. Commented Sep 18, 2019 at 13:17
  • Its a Class management system project. After selecting the option 8 it will enroll the student to class. and it will print the output as below Student S001, Kitty Tan enrolled into class C001, CCW101, Computer and Information Processing * Commented Sep 18, 2019 at 13:17

3 Answers 3

1

If you give the design of the .csv i guess you can use Pandas to make the access simpler. But i guess just store the string and print it later. and use break to end for when you found your student id.

Sign up to request clarification or add additional context in comments.

4 Comments

Pandas seems to be very heavy topic for me as Im a newbie. Please help me with the code.
I guess your question is already answered in Zhenhirs Post below. save the information needed in first loop, like studInfo = 'Student ' + str(Stud_id) + str(row[1]) break and then use print in second loop like print(studInfo + ' Enrolled into Class ' + str(Clas_id) + str(row[1]) + ',' + str(row[2])) break
yes but still facing some issue with the second loop. rectifyin the same.
Sorry but it is quite difficult help you like this, maybe share the data you are loading or a sample. I guess you are running into an Exception, or the Class ID is not found
0

Your code looks like it almost does what you want.. but you can store the student text "Student S001, Kitty Tan enrolled into class C001" in a variable and just print it later.. you could also try using end="" in your first print statement, which would stop it from printing a newline.. but I don't think that's as good a way to do this and it's only do-able in python3.

        Stud_id = str(input("Enter Student Id : ")).lower()
        Clas_id = str(input("Enter Class ID : ")).lower()

        Stud_File=open('Student_Data.csv', "r")                             ## Reading CSV file.
        Stud_Reader=list(csv.reader(Stud_File, delimiter=','))            ## Converting the csv file into List.
        Class_File=open('Class_Data.csv', "r")                             ## Reading CSV file.
        Class_Reader=list(csv.reader(Class_File, delimiter=','))            ## Converting the csv file into List.

        for row in Stud_Reader:                                               ## Creating a for loop to search sub-string in a string in a CSV file data.
                for field in row:                                
                        if Stud_id in field:                                        
                                student_text = "Student " + Stud_id + row[1] + " Enrolled into Class " + Clas_id


        for row in Class_Reader:
                for field in row:
                        if Clas_id in field:
                                print(student_text, row[1], row[2]) 

4 Comments

please rectify the code, as im a newbie in python. its is my college project.
He is just printing the string student_text in the last printing statement, so all wanted information gets out in one line. Besides that your solution and his is basically the same.
All the changes you should need are already in the code I posted above.. Did something not work?
Its not entering into second loop. it shows no output. returning back to main menu.
0

if you give your sample csv, it will be faster to understand. Below will work for your problem.

def check_student_id(student_id):
    try:
        Stud_File = open('Student_Data.csv', "r")
        Stud_Reader = list(csv.reader(Stud_File, delimiter=','))
        for stud_id_row in Stud_Reader:
            for stud_id in stud_id_row:
                if str(stud_id).lower() == student_id:
                    return True
        return False
    except Exception as Err:
        print(str(Err))
        return False


def check_class_id(class_id):
    try:
        Class_File = open('Class_Data.csv', "r")
        Class_Reader = list(csv.reader(Class_File, delimiter=','))
        for class_id_row in Class_Reader:
            for cls_id in class_id_row:
                if str(cls_id).lower() == class_id:
                    return True
        return False
    except Exception as Err:
        print(str(Err))
        return False

Stud_id = str(input("Enter Student Id : ")).lower()
Clas_id = str(input("Enter Class ID : ")).lower()

is_student_id_exists = check_student_id(Stud_id)
if is_student_id_exists:
    is_class_id_exits = check_class_id(Clas_id)
    if is_class_id_exits:
        print("Student {}, Enrolled into Class {}".format(Stud_id, Clas_id))
    else:
        print("Class id doesnt exits.")
else:
    print("Student id doesnt exits.")

if you CSV have data of student ID and class registered then we need to retrieve that data to show. But what i assume for you question is "Need to validate student ID and class ID present before proceeding to enrolling"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.