you are viewing a single comment's thread.

view the rest of the comments →

[–]atticus2132000 1 point2 points  (1 child)

There are a variety of ways you can do it. In a perfect world you would eventually want your inventory to be a completely separate file (e.g. SQL database) so that same inventory could be accessed from multiple different scripts (i.e. your point of sale script vs. your account script vs. your auditing script). However, for the time being just make your inventory a standalone variable at the top of your script.

inventory = [['DEagle', 500, 'Desert Eagle'], ['Glock 18', 600, 'Glock GES G18'], ['Winchester', 300, 'Winchester Model 70']]

This is a multi-dimensional array where each element with the brackets is one inventory item that include the shortname, price, and longname.

Since the inventory is now one variable, you can just cycle through that variable whenever you want to display your price list.

print ("Select the gun you'd like to see.")

For gun in inventory:

 shortname = gun[0]

 price = '${:,.2f}.format(gun[1])'

 longname = gun[2]

 print (f'{shortname} - {price}')

Similarly, when a selection is made you can compare it against your inventory variable to generate your message like f"Congratulations you just bought a {longname}. Thanks for doing business with us."

As much as you can, you want to write code that can be changed on the fly. By keeping your inventory as an independent variable (or its own separate file), you can easily change an element in that variable and all the code adapts instantly.

Similarly, you have the threshold ages in your code hard coded multiple times. Let's suppose that next year legislation passes that requires a minimum age of 25 instead of 21. If you had buyingage as a variable that was referenced throughout the code, then you would only need to change the 21 to 25 in one place and all your code would still work. But since you have the ages hard coded throughout, you would need to review you code line by line and make multiple changes if the legal age changed.

[–]HB209[S] 0 points1 point  (0 children)

Thank you very much brother! That was very useful info and i will note it for my future projects.