Kevin Nisbet
Kevin Nisbet

Reputation: 179

Using classes to print out instance attributes based on user input in Python

I am new to programming and would like to practice by creating a small app, using Python. The app asks the user to enter a level and a term and the app will return the curriculum for the said level and term.

I have tried before without using classes and only using nested dictionaries but it got really messy really quick as some classes have a different curriculum.

How could I check if the users input is inside an instance then return the instance attributes in which it contains?

Thanks!

class PacketClasses:

    def __init__(self, level, term, song, story, act1, act2):
        self.level = level
        self.term = term
        self.song = song
        self.story = story
        self.act1 = act1
        self.ac2 = act2


class HeadWayClasses:

    def __init__(self, level, term, song, review, expression, gs):
        self.level = level
        self.term = term
        self.song = song
        self.review = review
        self.expression = expression
        self.gs = gs


p1 = Packet("Pink", 1, "heads shoulders knees and toes", "The giant grass-hopper", "bowling", "craft")
p2 = Packet("Pink", 2, "ABC song", "The giant grass-hopper part 2", "shopping", "craft")
g1 = Packet("Green", 1, "ABC song", "Snow White", "Animals", "craft")
g2 = Packet("Green", 2, "ABC song", "Snow White part 2", "At the zoo", "craft")

o1 = HeadWay("Orange", 1, "song 1", "review 1", "expression 1", "gs 1")
o2 = HeadWay("Orange", 2, "song 2", "review 2", "expression 2", "gs 2")
a1 = HeadWay("Aqua", 2, "song 1", "review 1", "expression 1", "gs 1")
a2 = HeadWay("Aqua", 2, "song 2", "review 2", "expression 2", "gs 2")

users_level = input("Please enter the level you wish to check: ")
users_term = int(input("Please enter the term level you wish to check: "))

# if users_level & users_term in a class, print the curriculum

Upvotes: 1

Views: 113

Answers (1)

Michael Bianconi
Michael Bianconi

Reputation: 5232

This is a classic example of how inheritance and object-oriented programming can really help you out. We're going to create a parent class Classes and extend it. This will reduce a lot of code.

class Classes:

    def __init__(self, level, term, song):
        self.level = level
        self.term = term
        self.song = song

Now, we're going to extend it with the classes you already have:

class Packet(Classes):

    def __init__(self, level, term, song, story, act1, act2):
        super().__init__(level, term, song)
        self.story = story
        self.act1 = act1
        self.ac2 = act2


class HeadWay(Classes):

    def __init__(self, level, term, song, review, expression, gs):
        super().__init__(level, term, song)
        self.review = review
        self.expression = expression
        self.gs = gs

Not much has changed, but now you can be sure that any time you use either PacketClasses or HeadWayClasses interchangably, they'll always have a level, term, and song.

Now it's a simple matter of searching through classes for level and term. For really large amount of data, you'd actually want to use a real database. However, for small data like this, simply looping through is adequate.

Put the classes into an array:

classes = [
    Packet("Pink", 1, "heads shoulders knees and toes", "The giant grass-hopper", "bowling", "craft"),
    Packet("Pink", 2, "ABC song", "The giant grass-hopper part 2", "shopping", "craft"),
    Packet("Green", 1, "ABC song", "Snow White", "Animals", "craft"),
    Packet("Green", 2, "ABC song", "Snow White part 2", "At the zoo", "craft"),
    HeadWay("Orange", 1, "song 1", "review 1", "expression 1", "gs 1"),
    HeadWay("Orange", 2, "song 2", "review 2", "expression 2", "gs 2"),
    HeadWay("Aqua", 2, "song 1", "review 1", "expression 1", "gs 1"),
    HeadWay("Aqua", 2, "song 2", "review 2", "expression 2", "gs 2")
]

Now we can loop over them. Let's put the logic into a small function:

def findByLevelAndTerm(classes, level, term):
    for c in classes:
        if c.level == level and c.term == term:
            return c
    return None

And we're done! Now you can search the classes by level and term:

users_level = input("Please enter the level you wish to check: ")
users_term = int(input("Please enter the term you wish to check: "))
found_class = findByLevelAndTerm( classes, users_level, users_term )
if found_class is None: print("Unable to find the class!")

It's important to note that findByLevelAndTerm() will return only the first result. If multiple exist, you may want it to return a list instead.

Upvotes: 2

Related Questions