I have written a program where a user get shown three types of bird food. Each bird food comes in 10kg and 50kg bags each with different prices. The prices are stored in a list:

Pellets = ['Pellets', 22.75, 100.00]
Mash = ['Mash', 20.50, 90.00]
Enhanced = ['Enhanced', 25.50, 125.50]
Size = ['10KG', '50KG']

I have current code where a user inputs their choice of feed, what size and how many bags. It then calculates the price by how many bags:

if ((foodtype) is '1' and (bagsize) is '1'):
    print('$',((NumberOfBags)*Pellets[1]))

I am needing to write a if statement for a user to input there total amount of bird feed required and it tells how many bags of 50kg required and then the left over to be calculated into 10kg bags and for it to round up to a 10kg bag if a user inputs 52kgs required.

I expect the code to end up writing how many 50kg bags I need and how many 10kg bags I will require. e.g user says they need 134kg go bird feed the program will say you need 2 50kg bags and 4 10kg bags. Once this is worked out I plan to write how much the output will cost.

2 Answers

0
mfitzp On Best Solutions

An if statement isn't the way to go about this. What you're describing is simple arithmetic.

kgs_required = 52

First do integer division to get the number of full 50kg bags.

n_50kg_bags = kgs_required // 50

This will give you 1x 50kg bag. Next get the modulo (remainder) from that division.

kg_remaining = kgs_required % 50

This will give you the 2kg left over. Now calculate the number of 10s in this number, rounding up with math.ceil

n_10kg_bags = math.ceil(kg_remaining / 10)

Which will give you 1x again. So for 52kg you require 1x 50kg and 1x 10kg bags.

0
Wayne Van Son On

I feel like perhaps there may be a better way to hold all these items, such as a list of dictionaries.

Consider each dictionary a real life object, and all it's contents to be properties (each property has a name and a corresponding value). If I want pellets, I would search for name = pellets and get the price value

For this, I would assume that each object is a product and it has all it's properties listed inside.

list_of_products = [
  { 'name': 'pellets', 'weight': 10, 'price': 22.75},
  { 'name': 'pellets', 'weight': 50, 'price': 100.00},
  ... more products
]

This way you can query this list and get what is selected, as shown by this article

I use Javascript more than Python, so here is the answer that makes sense to me (not the highest votes):

def search(name, people):
    return [element for element in people if element['name'] == name]