First Steps in Coding: Going on a field-trip

The very first code I crafted can help educators (and even event-organizers) coordinate an outing/event for a group of people. The code automatically assigns the person to the relevant group and its respective activity according to the age of the person (the criteria can be adapted and instead of age, any other variable can be used). After a new member is added (with their name, age, and allergies), the list of the group is updated and shows the name and total number of members of each group as well as how many seats are left. The appropriate lunch is requested based on the person’s food allergies.

class Student:
    studCounter = 0
    
# class function with the main attributes
    def __init__(self, name, age, level, allergies):
        self.name = name
        self.age = age
        self.level = level # A to C
        self.allergies = allergies
        Student.studCounter += 1

# function that displays the total count of students
    def displayCount(self):
        print("Total number of students: %d" % Student.studCounter)

# function that displays the student's information 
    def displayStudent(self):
        print("Name: ", self.name, " Age: ", self.age, " Level: ", self.level, " Allergies: ", self.allergies)

# function that assigns the kid to the relevant group according to their age
    def activity(self):
        """Return the activity that the kid will engage in according to their age"""
        if self.age <= 3:
            return "{} will play". format(self.name)
        elif self.age <= 5:
            return "{} will swim". format(self.name)
        else:
            return "{} will hike". format(self.name)

# method for selecting the right lunch for allergic kids

    def lunch(self):
        """Return the lunch needed depending if the kid has an allergy or not"""
        if not self.allergies == "None":
            return "Special lunch needed"
        else:
            return "Regular lunch"

class Groups:
    groupACounter = 0
    groupBCounter = 0
    groupCCounter = 0

# class function with its main attributes
    def __init__(self, name, level, max_students):
        self.name = name
        self.level = level
        self.max_students = max_students
        self.students = []


# method that adds students to the lists of groups

    def add_student(self, student):
        if len(self.students) < self.max_students and self.level == "A":
            self.students.append(student)
            self.max_students = self.max_students - 1
            Groups.groupACounter += 1
        elif len(self.students) < self.max_students and self.level == "B":
            self.students.append(student)
            self.max_students = self.max_students - 1
            Groups.groupBCounter += 1
        else:
            self.students.append(student)
            self.max_students = self.max_students - 1
            Groups.groupCCounter += 1
            return True
        return self

    def display_count_groupA(self):
        print("Total number of students in group A: %d" % Groups.groupACounter)

    def display_count_groupB(self):
        print("Total number of students in group B: %d" % Groups.groupBCounter)

    def display_count_groupC(self):
        print("Total number of students in group C: %d" % Groups.groupCCounter)
import Students as stud

# instance of the class Students
student1 = stud.Student("Ani", 3, "A", "nuts")
student1.displayStudent()
print(student1.activity())
print(student1.lunch())

print("**********************************")
# instance of the class Students
student2 = stud.Student("George", 5, "C", "None")
student2.displayStudent()
print(student2.activity())
print(student2.lunch())

print("**********************************")
student3 = stud.Student("Sam", 3, "A", "shrimp")
student3.displayStudent()
print(student3.activity())
print(student3.lunch())

student4 = stud.Student("Bill", 4, "B", "None")
student4.displayStudent()
print(student4.activity())
print(student4.lunch())
student4.displayCount()

groupA = stud.Groups("Playing", "A", 10)
groupB = stud.Groups("Swimming", "B", 15)
groupC = stud.Groups("Hiking", "C", 15)

groupA.add_student(student1)
groupC.add_student(student2)
groupA.add_student(student3)
groupB.add_student(student4)
print("************* GROUP A *********************")
print("Activity: ", groupA.name)
print("Number of spots available in Group A: ", groupA.max_students)
print("Kids enrolled: ")
for member in groupA.students:
    print(member.name)

print("************* GROUP B *********************")
print("Activity: ", groupB.name)
print("Number of spots available in Group B: ", groupB.max_students)
print("Kids enrolled: ")
for member in groupB.students:
    print(member.name)

print("************* GROUP C *********************")
print("Activity: ", groupC.name)
print("Number of spots available in Group C: ",groupC.max_students)
print("Kids enrolled: ")
for member in groupC.students:
    print(member.name)

groupA.display_count_groupA()
groupB.display_count_groupB()
groupC.display_count_groupC()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s