#Create a list of books with titles, prices, quantity, etc.

9 messages · Page 1 of 1 (latest)

gaunt aurora
#

As my first official Python project, I am creating an online bookstore. It is something relatively simple to practice my skills. I want to create a list where I can store the books available in the bookstore. It needs to have the book title, the price and the quantity of books available without a database. How can I do that?

exotic flint
#

What part are you struggling with exactly?

gaunt aurora
exotic flint
#

I think you're looking for classes (aka dataclasses in this case)

winter hedge
#

Or do a list of dictionaries 💀

sterile finch
#

As my first official Python project, I am creating an online bookstore. It is something relatively simple to practice my skills. I want to create a list where I can store the books available in the bookstore. It needs to have the book title, the price and the quantity of books available without a database. How can I do that?

There are multiple ways to solve your problem, with their own pros and cons. If you have not yet learned object-oriented programming, you can create a list of dictionaries like wang said. Each dictionary would represent a single book.

However, it would be better to use object-oriented programming by creating a Book class, so that you can name the class and work with objects that represent books. This is nice because you can also create methods on the Book class for things like updating the price and quantity, instead of having to figure out how to edit a dictionary in a list of dictionaries. It can make for more readable and simpler looking code.

If you haven't learned OOP yet, I would look for a good beginner tutorial about it using Python, online, and work through that first so you learn the basic concepts of classes and methods, before applying it to the book store app.

prisma rune
#

I used to care about whether I'll do it OOP, functional, or not, clean code, best practices.

Honestly? Just do it first. Make it work. Come back for a code review, and find out approaches based on what you wrote.

Asking the question from ground zero is just perfectionism and ground for procrastination. There's no professor around. No one's gonna give you an F. The point is to get your hands in the sand and get used with building castles in the sand.

mortal crow
#

there's a bajillion ways to do this:

  • three lists, all the same length, one for titles, one for prices and one for quantities (you'd need to make sure that index n in each list referred to the same book)
  • a list of lists (or tuples) where each item in the outer list is a list or a tuple of length 3 where you know that the first item is the title, second is price, third is quantity
  • same as above but a list of dictionaries instead of lists, this means you don't have to keep the order correct in each inner list (and you don't have to provide every value for every book)
  • a list of (data)classes, cleaner than dictionaries, will help with autocomplete..., will be more code to write (*in the simplest case, depending how you need to use it you could save on lines in the long run)

I've listed these in order of complexity where the first ideas are the simplest to implement but they're the easiest to make mistakes while using them in your code

#

whichever option you go for you'll learn stuff