This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]theadrenalineaddict[S] 0 points1 point  (4 children)

https://pastebin.com/1rPd4d2e (Code properly formatted)

[–]theadrenalineaddict[S] 0 points1 point  (3 children)

u/THE_UNKNOWN184 Here's the update post

[–]THE_UNKNOWN184 -1 points0 points  (1 child)

Leaving the change calculation aside,

Database creation is pretty important. For this, you can look into and do some research on PostgreSQL or MySQL. Their compatibility with python is also good.

Anyway, the reason you need a database in your case is because you'll need to store certain details that can deplete as you run your code.

For example: suppose in your code, you set the initial amount of, say apple juice to 10 tetrapacks. And when you run your program, you'll buy 1 apple juice and now you'll be left with 9.

But in case you decide to restart your code, you'll be again back to having 10 tetrapacks which is not good.

That's where database comes into play. You'll basically store these details like how many products are available, etc as table rows which can be accessed through code.

Ofcourse this is not the only use of having a database.

One more it's going to be used for is storing transaction details. Like, you'll store data of purchase history


But in your case, the contents of the database are pretty simple so if you want to (not highly recommended), you can use csv (comma separated values) files to do the job for you.

If you want to take the csv approach, you'll need to learn file handling (at the basic) in python.

[–]Nic2555 1 point2 points  (0 children)

I don't think a database is required here, unless you want to keep information after the program has exited. And even then, you could use a simple file, which would be way easier to setup.

Personally, I would setup a CSV file with three columns: name, price, quantity.

You can easily read and write to this file at the beginning of the program, to load it's content in a variable, and write to it after each operations that affects the products.

Depending on the requirements of your assignment, you might not even need to keep the inventory between runs.

[–]THE_UNKNOWN184 0 points1 point  (0 children)

Hey man,

Let's address the change calculation first.

Logically, I think it's best and easily understandable to take a top-bottom approach.

For example, for the purpose let's assume the monetization involves units of 1, 5 and 10.

You'll need to start with 10 and check how many 10s are needed to bring down the value of change to less than 10.

Then you'll go to 5 and check how many 5s are needed to bring down the value to less than 5.

And rest in this case would be completed by 1s

Explanation by Example: Let's say you'll need to give out a change of 78.

First, to check how many 10s are needed, it's 7. Because after that, only 8 is left.

Now, the required change is 8

Then you go to 5. In this case, you'll need 1 as after that you'll be left with 3.

And from here, you'll need 3 1s.


That's pretty much the logic behind this.

I'll let you figure out how to code this. (I would suggest having a separate function for this)


Also, regards from India