I need help with classes

Im doing a project now and while i succeeded doing it another way, i wanted to try it with classes but i never finished any project with classes except for anything that you “learn”, i feel i forgot most things unless i really try them myself with my own ideas. I’ve cut it down to a basic level to learn and see what i am doing wrong.

class Eggmaster: eggID = { "0": "Organic Egg", "1": "Free Range Egg", "2": "Barn Egg", "3": "Cage Egg", } def user_input(): egg = input("Enter the code on the egg: ") return egg # test out 1UK54321 or any code with 1-3 digits in the beginning def __init__(self, egg): self.egg = egg def country(self, eggID): print(eggID[self.egg[0]]) user_input()

I’m really confused to the point where im not knowing what im doing anymore. I got the challenge from this website: Egg Code Stamp Decoder | 101 Computing

do you see egg = input should be ‘egg = user_input’

i don’t really understand that if i look at it.

I’d recommend using if and elif statements or a dictionary for parts of this.
For example, you could have a get_country method:

    def get_country(self):
        code = self.egg[1:3]
        countries = {
           "UK": "United Kingdom",
           "FR": "France",
           "BE": "Belgium",
           "DE": "Germany",
           "ES": "Spain"
        return countries[code]

And to test it, I would do the code written below outside the class

master = Eggmaster("1UK54321")

You could even have a static method to create an egg object using user input.

class Eggmaster: 
   def __init__(self, egg):
     self.egg = egg

  def create_from_user_input():
    egg = input("Enter the code on the egg: ")
    return Eggmaster(egg)

To test all of that together, I might do

master = Eggmaster.create_from_user_input()
print("Country of Origin:", master.get_country())

outside the class.

1 Like

i already solved this without classes but i wanna get my head around it.

class Eggmaster: def __init__(self, egg): self.egg = egg def farming(self): code_part_one = self.egg[0] return code_part_one def test(self): eggID = { "0": "Organic Egg", "1": "Free Range Egg", "2": "Barn Egg", "3": "Cage Egg" } return eggID def egg_code(self, eggID, code_part_one): print(eggID[code_part_one]) master = Eggmaster("1UK54321") print(master.farming())

Why is this not working?!

It looks like you spread the getting the part_one stuff over multiple functions (methods of an Eggmaster object)

Your .farming method only returns the first character in the .egg string.
Nothing else happens in that function (from the code you have above).

You could call the other methods you have inside the .farming method to make things work the way you want.

    def farming(self):
        code_part_one = self.egg[0]
        eggID = self.test()
        return self.egg_code(eggID, code_part_one)

Or, you could combine that as just one method instead.

    def farming(self):
        code_part_one = self.egg[0]
        eggID = {
            "0": "Organic Egg",
            "1": "Free Range Egg",
            "2": "Barn Egg",
            "3": "Cage Egg"
        return eggID[code_part_one]

some notes on what you have so far:

  1. You could make the eggID dictionary a class variable, to make it easier to access for all the methods, or so that you don’t have a method whose only purpose is to return that dictionary.
  2. The test and egg_code methods don’t use self at all, so they don’t need to be methods inside the class, they would work as functions outside of it.
  3. You can eliminate some of the parameters by using other methods or properties or class variables.
    For example, the eggID parameter in egg_code is not needed if you use the .test method.
    def egg_code(self, code_part_one):
        egg_ID = this.test()
1 Like