allows you to add a new course to a list that contains the courses in which you are enrolled

Question:

courseList = list()
courseList = list()

class Student:
def __int__(self, studentCod, name):
self.studentCod = studentCod
self.name = name
class Courses:
def __int__(self, courseName):
self.courseName = courseName
def menu():
select = 0
while select != 5:
print("Welcome students")
print("choose one of the options")
print("----------------------------")
print("1. Add new students")
print("2. Show registered students")
print("3. Add course")
print("4. Student's registered courses")
print("5. Quit")
select = int(input("Choose an option: "))

if select == 1:
    addStudent()
if select == 2:
    to show()
if select == 3:
    addCourse()
if select == 4:
    findStudent()
if select == 5:
    leave()
def addStudent():
print("")
student = Student()
student.codStudent = input("Enter the student code: ")
student.name = input("Enter name: ")

listStudents.append(student)

def show():
for student in studentList:
 print("There are ", listStudents.__len__(), "students records")
 print("#", studentList.index(student) + 1)
 print("The student's name is: ", student.name)
 print("The student code is: ", student.StudentCode)
def addCourse():
 print("")
 codAlumno = input("Enter the student code: ")
 print("Looking for student registration...")
 print("Found")
 for student in studentList:
 if student.codStudent == codStudent:
    course = Courses()
    course.CourseName = input("Enter the course name: ")
 courseList.append(course)

def findStudent():
codAlumno = input("Enter the student code to see their registered courses: ")
 for student in studentList:
   if student.codStudent == codStudent:
    for course in courseList:
        print("The name of the course is:", course.courseName)
def exit():
print("See you later")

**hello good afternoon, I have the following problem of the title statement, I want my method of adding course and reporting to depend on registering new courses and not joining each other because I was doing tests, an example when having two registers of new students and add courses at the time of reporting, I get the courses of the first record plus the second. yes please help me to find the solution. **

Answers:

You are using the courseList as a global variable, so any courses which are added by any student will be shared by all the students.
Instead add the courseList as a member of the Student class first.

class Student:
def __int__(self, studentCod, name):
self.studentCod = studentCod
self.name = name
self.courseList = list()

Then add the courses to the respective student’s list.

for student in studentList:
 if student.codStudent == codStudent:
    course = Courses()
    course.CourseName = input("Enter the course name: ")
    student.courseList.append(course)
Answered By: PerennialLearning
studentList = list()
class Student:
def __int__(self, studentCode, name): # self reserved word to refer to itself
    self.studentCod = studentCod
    self.name = name
    self.CourseList = list()
class Courses:
def __int__(self, courseName):
    self.courseName = courseName
def menu():
select = 0
while select != 5:
    print("Welcome students")
    print("choose one of the options")
    print("----------------------------")
    print("1. Add new students")
    print("2. Show registered students")
    print("3. Add course")
    print("4. Student's registered courses")
    print("5. Quit")
    select = int(input("Choose an option: "))

    if select == 1:
        addStudent()
    if select == 2:
        to show()
    if select == 3:
        addCourse()
    if select == 4:
        findStudent()
    if select == 5:
        leave()
def addStudent():
student = Student()
print("")
student.codStudent = input("Enter the student code: ")
student.name = input("Enter name: ")

listStudents.append(student)
def show():
for student in studentList:
    print("There are ", listStudents.__len__(), "students records")
    print("#", studentList.index(student) + 1)
    print("The student's name is: ", student.name)
    print("The student code is: ", student.StudentCode)
def addCourse():
print("")
codAlumno = input("Enter the student code: ")
print("Looking for student registration...")
print("Found")
for student in studentList:
    if student.codStudent == codStudent:
        course = Courses()
        course.CourseName = input("Enter the course name: ")
        student.CourseList.append(course.courseName)
def findStudent():
codAlumno = input("Enter the student code to see their registered courses: ")
for student in studentList:
    if student.codStudent == codStudent:
        for course in listStudents:
            print("The name of the course is:", course.courseName)
def exit():
print("See you later")

# ------------------------------------------------- ------------------


menu()
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.