-1

I am new to Python and I am trying to make pizza order system project. I need to create a superclass called pizza then create subclasses called Klasik and Margherita. In each subclasses I define ingredients and cost. Then when someone orders a pizze I need to get that information. Here is my code:

#create superclass
class Pizza(object):
    def _init_(self, description, cost):
        self.description=" "
        self.cost=0
    def get_description(self):
        return self.description
    def get_cost(self):
        return self.cost


#create subclasses

class Klasik(Pizza):
    def _init_(self, description, cost):
      self.description = "Ingredients: cheese, olive, corn"
      self.cost = 60.0

class Margherita(Pizza):
    def _init_(self):
        self.description="Ingredients: tomato, mozarella "
        self.cost = 50.0

pizza = input("Please select 1 or 2")
    if pizza=='1':
        pizza=Klasik()
    elif pizza=='2':
        pizza=Margherita()
    else :
        print("Please choose between: 1 or 2")

pizza_cost = pizza.self.cost()
print("your pizza costs" + pizza_cost)

I look for attribute error pages but They are all so complicated. I looked at one of my friends codes who finished this project, we have very similar codes but when I select 1 I get

     AttributeError:'Klasik' object has no attribute 'self'

Thank you

  • `_init_` should be `__init__`, with double underscores. – Jonathan Ciapetti Mar 13 '23 at 12:58
  • And there is no need to access `self` when you already access an instance. It should just be `pizza_cost = pizza.cost` – Tomerikoo Mar 13 '23 at 12:59
  • I don't know if this is an exercise or a self-project, but there is absolutely no benefit in using a sub-class here. They don't define anything that doesn't already exist in the super class. You might as well just do `margherita = Pizza("Ingredients: tomato, mozarella", 50)` – Tomerikoo Mar 13 '23 at 13:03
  • There is no point to the superclass in the OP code. No inherited attributes are ever used. – user19077881 Mar 13 '23 at 13:15

2 Answers2

0

Indeed self is not an attribute of your class.

If you want to access pizza_cost, just access it by writing what follows:

pizza_cost = pizza.get_cost()
WArnold
  • 275
  • 2
  • 10
-1

if you use description and cost as parameter, then define the default values first not in the function. Also, you are using pizza as an instance of classes and a variable. This can cause misunderstanding. Cost is an attribute therefore you can access it by pizza.cost notation, not like a function call of pizza.cost().I think this is proper way but you need to improve it :

#create superclass
class Pizza(object):
    def _init_(self, description, cost):
        self.description=" "
        self.cost=0
    def get_description(self):
        return self.description
    def get_cost(self):
        return self.cost


#create subclasses

class Klasik(Pizza):
    def __init__(self, description="None", cost=60.0):
        
      self.description = description
      self.cost = cost

class Margherita(Pizza):
    def __init__(self, description="None", cost=50.0):
        
      self.description = description
      self.cost = cost

pizza = input("Please select 1 or 2")

if pizza=='1':
    pizza=Klasik()
elif pizza=='2':
    pizza=Margherita()
else :
    print("Please choose between: 1 or 2")

pizza_cost = pizza.cost
print("your pizza costs " + str(pizza_cost))
Ugur Yigit
  • 155
  • 7