- Create the base classes for the store.
- Interact with the classes by making some objects and running basic logic on them.
- Learn how to make a console app wait for certain input before exiting a loop.
- Clone this repo. Ensure that you have a
main.py
that contains an emptymain()
. - Add the entrypoint for running your
main.py
by adding the following to the bottom of the file:if __name__ == "__main__": main()
- Add a class to your project called
Product
. You can add it directly into themain.py
file if you want to; remember it needs to be outside ofmain()
. This class will be what's called a base class or a parent class. This will be the most basic class that all product classes will inherit from. Ref: Python docs on classes - Add a constructor to your
Product
class by creating a method inside called__init__
by doing:def __init__(self):
.- Note: Remember that
self
must be the first parameter for your constructor. This allows the single instance to reference it's own variables and methods, i.e. "When one cat meows, not all cats meow."
- Note: Remember that
- Add the following parameters to your
__init__()
constructor:- name - Type
str
- price - Type
float
- quantity - Type
int
- description - Type
str
Example is as follows:
def foo(bar1: str, bar2: float, bar3: int): ...
- name - Type
- Inside your
__init__()
constructor please assign the passed arguments to instance variables; this will make sure the arguments you pass in are available in the instance:def foo(bar1: str, bar2: float, ...): self.bar1 = bar1 ...
- Add another class called
CatFood
. Make this class inherit from the base classProduct
. Example:# FooClass inherits from BarClass class FooClass(BarClass): ...
- Please give
CatFood
a few Properties. You can do so by following the same process of building a constructor__init__()
inside ofCatFood
and assigning it's arguments to instance variables as you did withProduct
.- WeightPounds - Type
float
- KittenFood- Type
bool
- WeightPounds - Type
- Make another class called
DogLeash
and have it inherit fromProduct
as well. Give it a the following Properties- LengthInches - Type
int
- Material - Type
str
- LengthInches - Type
Now we want to get this app to run until the user decides to end it. We will do this by using a while
loop. Loops require 3 things: an inital condition, check in condition, and a change in condition. Our inital condition is going to be our first input from the user. Our check is what is in the while loop. Our change is going to be what the user inputs after an action has been complete. So, let's get started with the initial condition.
- Inside our
main()
inmain.py
, add the following lines:print("Press 1 to add a product")
and below that addprint"Type 'exit' to quit")
. - Add this line of code directly below your
print()
lines:userInput = str(input("Option: "))
. This will get what the user enters before hitting enter. - Now add your while loop which typically in the form of
while( <some conditional expression> )
, e.g.while(some_variable < 5)
. The condition in the loop should be something like this:userInput.lower() != "exit"
. We use the methodlower()
just in case the user enters "Exit" instead of "exit". - Inside the body of the while loop, at the bottom of the loop, add this line
userInput = str(input("Option: "))
. This is our change in condition. We will add code before this though, so it will make more sense after that. Add the instructions for the console app above that line again:print("Press 1 to add a product")
andprint("Type 'exit' to quit")
. - Inside of the while loop, above the latest
print()
andinput()
lines you added in the previous step, add anif
statement. This if statment will check the input of the user a second time to see if they wanted to add a product. We told them to enter "1" to add a product, so check for that in the if statment. Remember, the input is astr
type, not anint
. - Create a new object. It should be
CatFood
orDogLeash
, not the base classProduct
. Ref: An example code snippet on making classes & objects - Since we don't really have a database, when we add a product, we're going to just create that object and print it to the console. Use
print()
andinput()
to get data from the user and then add it to the object you just created. - To make printing the object easier, we'll use
json.dumps()
andprint()
. You don't need to know all the ins and outs of how this works right now, but basically it will just convert our object to a json object. Add the following line to your code:print(json.dumps(dogLeash))
. You will replacedogLeash
with whatever you named your object. - Now, all you have to do is test it! Run the application and see if everything works properly.