Python Forum

Full Version: Grocery List Program
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am doing a program where you input the amount of people in your family and the ages and Gender and then it asks what do you want to spend on grocery for a certain time span. I need help figuring out how to make a algorithm to calculate the grocery list and then print it out to the user.


#Tristan Fish
#Mr.Reid
#ICS3U
#12/19/2017
#12//2017
import random
#Food Groups
foodGroupGrains = "Grains"
foodGroupFruitsAndVegtables = "Fruits And Vegtables"
foodGroupDairy = "Dairy"
foodGroupProteins = "Proteins"

#totalCaloriesStore
#totalPriceStore





#def Breakfast_Food(test):
    
        
    


#Breakfast Foods
#Layout Cost,Servings,Calories,Food Group 
dempstersWholeWheatBread = [ "Dempsters Whole Wheat Bread",2.27,18,1620,foodGroupGrains]
dempstersWhiteBread =  ["Dempsters White Bread",2.27,18,1620,foodGroupGrains]
dempstersEverythingBagels = ["Dempsters Everything Bagels",2.98,12,1200,foodGroupGrains]
galaApple = ["Gala Apples",0.71,1,52,foodGroupFruitsAndVegtables]
banana = ["Bananas",0.41,1,105,foodGroupFruitsAndVegtables]
clementines = ["Clementines",2.97,20,700,foodGroupFruitsAndVegtables]
greatValueLargeEggs = ["Great Value Large Eggs",2.27,6,840,foodGroupProteins]
nelsonMilk = ["Nelson Milk",4.27,16,2080,foodGroupDairy]
greatValueOldCheddarCheese = ["Great Value Old Cheddar Cheese",4.97,5,1800,foodGroupDairy]
GreatValueVanillaYogurt = ["Great Value Vanilla Yogurt",1.77,6.5,600,foodGroupDairy]
GreatValueOrangeJuice = ["Great Value Orange Juice",1,4,440,foodGroupFruitsAndVegtables]



def Calculator():
    foodFoodGroups =  [dempstersWholeWheatBread[4],dempstersWhiteBread[3],dempstersEverythingBagels[4],galaApple[4],banana[4],clementines[4],greatValueLargeEggs[4],nelsonMilk[4],greatValueOldCheddarCheese[4],GreatValueVanillaYogurt[4],GreatValueOrangeJuice[4]]
    foodCalories =  [dempstersWholeWheatBread[3],dempstersWhiteBread[3],dempstersEverythingBagels[3],galaApple[3],banana[3],clementines[3],greatValueLargeEggs[3],nelsonMilk[3],greatValueOldCheddarCheese[3],GreatValueVanillaYogurt[3],GreatValueOrangeJuice[3]]
    foodServings = [dempstersWholeWheatBread[2],dempstersWhiteBread[2],dempstersEverythingBagels[2],galaApple[2],banana[2],clementines[2],greatValueLargeEggs[2],nelsonMilk[2],greatValueOldCheddarCheese[2],GreatValueVanillaYogurt[2],GreatValueOrangeJuice[2]]
    foodCost  = [dempstersWholeWheatBread[1],dempstersWhiteBread[1],dempstersEverythingBagels[1],galaApple[1],banana[1],clementines[1],greatValueLargeEggs[1],nelsonMilk[1],greatValueOldCheddarCheese[1],GreatValueVanillaYogurt[1],GreatValueOrangeJuice[1]]
    foodNames = [dempstersWholeWheatBread[0],dempstersWhiteBread[0],dempstersEverythingBagels[0],galaApple[0],banana[0],clementines[0],greatValueLargeEggs[0],nelsonMilk[0],greatValueOldCheddarCheese[0],GreatValueVanillaYogurt[0],GreatValueOrangeJuice[0]]
    print random.choice(foodNames), random.choice(foodCost),random.choice(foodServings),random.choice(foodCalories)



Calculator()
def Grocery_List(test):
    if timeFrame == "One Week" and moneySpent >50 and moneySpent <100 and peopleInFamly == 1 and agesOfPeopleInFamly11 >5 and agesOfPeopleInFamly11 <10 and genderOfPeople11 == "Male":
        print ("test")



#Lunch Foods
#Layout Cost,Servings,Calories,Food Group 
greatValueSlicedHam = [3.47 and 6  and 360  and foodGroupProteins]
pillarsClubSizeSmokedTurkeyBreast = [8 and 4  and 1200  and foodGroupProteins]
honeyGarlicMiniPepperoniSticks = [6.97 and 2.4  and 1400  and foodGroupProteins]
strawberryBananaYop = [0.98 and 1  and 150  and foodGroupDairy]
vanillaYop = [0.98 and 1  and 150  and foodGroupDairy]
strawberryYop = [0.98 and 1  and 150  and foodGroupDairy]
blueberryYop = [0.98 and 1  and 150  and foodGroupDairy]
minuteMaidAppleJuice = [2.97 and 10  and 1000  and foodGroupFruitsAndVegtables]
minuteMaidOrangeJuice = [2.97 and 10  and 1000  and foodGroupFruitsAndVegtables]
lunchMateHamStackers = [2.97 and 1  and 260  and foodGroupGrains]
toppablesCrackers = [2.47 and 4.5  and 2025  and foodGroupGrains]
ritzOriginalCrackers = [2.47 and 4  and 1000  and foodGroupGrains]
wheatThinsMultiGrain = [2.47 and 4  and 990  and foodGroupGrains]
mrNoodlesChicken = [0.37 and 1  and 380  and foodGroupGrains]
mrNoodlesBeef = [0.37 and 1  and 380  and foodGroupGrains]
                        
#Dinner Foods
#Layout Cost,Servings,Calories,Food Group 
bonelessChickenBreast = [10 and 5  and 800  and foodGroupProteins]
mapleLeafBonelessSkinlessChickenThighs = [10.37 and 10  and 1200  and foodGroupProteins]
mapleLeafPorkBoneless = [10 and 6  and 1020  and foodGroupProteins]
greatValueBoneLessSmokedHam = [10 and 15  and 1530  and foodGroupProteins]
leanGroundBeefTube = [4.50 and 3  and 210  and foodGroupProteins]
OneWorldHalelEyeRoundFastFrySteak = [8 and 4  and 840  and foodGroupProteins]
greatValueMacaroniAndCheese = [0.48 and 2  and 840  and foodGroupGrains]
lanciaReadyCutMacaroni = [1.27 and 6  and 3360  and foodGroupGrains]
greatValueWholeKernalCorn = [0.77 and 2  and 150  and foodGroupFruitsAndVegtables]
heinzAlphagettiPasta = [1.27 and 1  and 510  and foodGroupGrains]
carrots = [1.47 and 3  and 558  and foodGroupFruitsAndVegtables]
sweetOnions = [3.97 and 6  and 435  and foodGroupFruitsAndVegtables]
redOnions = [3.97 and 6  and 1260  and foodGroupFruitsAndVegtables]






#User Inputs
while True:
    try:
        peopleInFamly = int(input("How Many People Are In Your Famly "))
        # Amount Of Weeks Do You wanna Buy Groceries For
        

    except ValueError:
        print("Invalid Input Please Try Again")
        continue
    else:
        break
#If There Is Only 1 Person In your famly 
if peopleInFamly == 1:
    (Calculator())
    agesOfPeopleInFamly11 = int(input("How Old Are You "))
    genderOfPeople = input("What Gender Are You ") 
#If There Is 2 People In your famly
elif peopleInFamly == 2:
    agesOfPeopleInFamly12 = int(input("How Old Is the first person in your famly "))
    genderOfPeople12 = input("What Gender is the first person in your famly ")
    agesOfPeopleInFamly22 = int(input("How Old Is the Second person in your famly "))
    genderOfPeople = input("What Gender is the Second person in your famly ")
#If There Is 3 People In your famly
elif peopleInFamly == 3:
    agesOfPeopleInFamly13 = int(input("How Old Is the first person in your famly "))
    genderOfPeople = input("What Gender is the first person in your famly ")
    agesOfPeopleInFamly23 = int(input("How Old Is the Second person in your famly "))
    genderOfPeople = input("What Gender is the Second person in your famly ")
    agesOfPeopleInFamly33 = int(input("How Old Is the Third person in your famly "))
    genderOfPeople = input ("What Gender is the Thrid person in your famly ")
#If There Is 4 People In your famly
elif peopleInFamly == 4:
    agesOfPeopleInFamly14 = int(input("How Old Is the first person in your famly "))
    genderOfPeople = input("What Gender is the first person in your famly ")
    agesOfPeopleInFamly24 = int(input("How Old Is the Second person in your famly "))
    genderOfPeople = input("What Gender is the Second person in your famly ")
    agesOfPeopleInFamly34 = int(input("How Old Is the Third person in your famly "))
    genderOfPeople = input("What Gender is the Thrid person in your famly ")
    agesOfPeopleInFamly44 = int(input("How Old Is the Fourth person in your famly "))
    genderOfPeople = input("What Gender is the Fourth person in your famly ")
#If There Is 5 People In your famly
elif peopleInFamly == 5:
    agesOfPeopleInFamly15 = int(input("How Old Is the first person in your famly "))
    genderOfPeople = input("What Gender is the first person in your famly ")
    agesOfPeopleInFamly25 = int(input("How Old Is the Second person in your famly "))
    genderOfPeople = input("What Gender is the Second person in your famly ")
    agesOfPeopleInFamly35 = int(input("How Old Is the Third person in your famly "))
    genderOfPeople = input("What Gender is the Thrid person in your famly ")
    agesOfPeopleInFamly45 = int(input("How Old Is the Fourth person in your famly "))
    genderOfPeople = input("What Gender is the Fourth person in your famly ")
    agesOfPeopleInFamly55 = int(input("How Old Is the Fifth person in your famly "))
    genderOfPeople = input("What Gender is the Fifth person in your famly ")
#If There Is 6 People In your famly
elif peopleInFamly == 6:
    agesOfPeopleInFamly16 = int(input("How Old Is the first person in your famly "))
    genderOfPeople = input("What Gender is the first person in your famly ")
    agesOfPeopleInFamly26 = int(input("How Old Is the Second person in your famly "))
    genderOfPeople = input("What Gender is the Second person in your famly ")
    agesOfPeopleInFamly36 = int(input("How Old Is the Third person in your famly "))
    genderOfPeople = input("What Gender is the Thrid person in your famly ")
    agesOfPeopleInFamly46 = int(input("How Old Is the Fourth person in your famly "))
    genderOfPeople = input("What Gender is the Fourth person in your famly ")
    agesOfPeopleInFamly56 = int(input("How Old Is the Fifth person in your famly "))
    genderOfPeople = input("What Gender is the Fifth person in your famly ")
    agesOfPeopleInFamly66 = int(input("How Old Is the Sixth person in your famly "))
    genderOfPeople = input("What Gender is the Sixth person in your famly ")
#If There Is 7 People In your famly
elif peopleInFamly == 7:
    agesOfPeopleInFamly17 = int(input("How Old Is the first person in your famly "))
    genderOfPeople = input("What Gender is the first person in your famly ")
    agesOfPeopleInFamly27 = int(input("How Old Is the Second person in your famly "))
    genderOfPeople = input("What Gender is the Second person in your famly ")
    agesOfPeopleInFamly37 = int(input("How Old Is the Third person in your famly "))
    genderOfPeople = input("What Gender is the Thrid person in your famly ")
    agesOfPeopleInFamly47 = int(input("How Old Is the Fourth person in your famly "))
    genderOfPeople = input("What Gender is the Fourth person in your famly ")
    agesOfPeopleInFamly57 = int(input("How Old Is the Fifth person in your famly "))
    genderOfPeople = input("What Gender is the Fifth person in your famly ")
    agesOfPeopleInFamly67 = int(input("How Old Is the Sixth person in your famly "))
    genderOfPeople = input("What Gender is the Sixth person in your famly ")
    agesOfPeopleInFamly77 = int(input("How Old Is the Seventh person in your famly "))
    genderOfPeople = input("What Gender is the Seventh person in your famly ")
#If There Is 8 People In your famly
elif peopleInFamly == 8:
    agesOfPeopleInFamly18 = int(input("How Old Is the first person in your famly "))
    genderOfPeople = input("What Gender is the first person in your famly ")
    agesOfPeopleInFamly28 = int(input("How Old Is the Second person in your famly "))
    genderOfPeople = input("What Gender is the Second person in your famly ")
    agesOfPeopleInFamly38 = int(input("How Old Is the Third person in your famly "))
    genderOfPeople = input("What Gender is the Thrid person in your famly ")
    agesOfPeopleInFamly48 = int(input("How Old Is the Fourth person in your famly "))
    genderOfPeople = input("What Gender is the Fourth person in your famly ")
    agesOfPeopleInFamly58 = int(input("How Old Is the Fifth person in your famly "))
    genderOfPeople = input("What Gender is the Fifth person in your famly ")
    agesOfPeopleInFamly68 = int(input("How Old Is the Sixth person in your famly "))
    genderOfPeople = input("What Gender is the Sixth person in your famly ")
    agesOfPeopleInFamly78 = int(input("How Old Is the Seventh person in your famly "))
    genderOfPeople = input("What Gender is the Seventh person in your famly ")
    agesOfPeopleInFamly88 = int(input("How Old Is the Eighth person in your famly "))
    genderOfPeople = input("What Gender is the Eighth person in your famly ")
    



 
timeFrame = input("How Many Weeks Would you like to buy Groceries For ")  
# Asks For How Much Money Wants To Be Spend For 1 Week On Groceries
if timeFrame == "One Week":
    moneySpent = int(input("How Much Would You Like To Spend On Groceries For 1 Week Including Taxes "))
        
# Asks For How Much Money Wants To Be Spend For 2 Weeks On Groceries
elif timeFrame == "Two Weeks":
    moneySpent = int(input("How Much Would You Like To Spend On Groceries For 2 Weeks Including Taxes "))
         
# Asks For How Much Money Wants To Be Spend For 3 Weeks On Groceries
elif timeFrame == "Three Weeks":
    moneySpent = int(input("How Much Would You Like To Spend On Groceries For 3 Weeks Including Taxes "))
         
# Asks For How Much Money Wants To Be Spend For 4 Weeks On Groceries   
elif timeFrame == "Four Weeks":
    moneySpent = int(input("How Much Would You Like To Spend On Groceries For 4 Weeks Including Taxes "))


if  timeFrame == "OneWeek" and  moneySpent <50 and moneySpent >25 and agesOfPeopleInFamly11 >14 and agesOfPeopleInFamly11 <18 and genderOfPeople11 == "Male":
    print ("Your Grocerie list is,  ")


    
The algorithm is independent of the code. In other words, you should be able to describe your algorithm in words and then think about implementation.

So you might say, "25% of the food budget will be spent on breakfast and lunch, and the remaining 50% will be spent on dinner foods. For each meal, we will maximize the budget while trying to buy as many different things as possible."

But let's talk about the code you have because I think that the code you've written is going to make things more difficult for you.

The biggest problem with your code right now is that depending on the number of people specified, you create different variables:

if peopleInFamly == 1:
    (Calculator())
    agesOfPeopleInFamly11 = int(input("How Old Are You "))
    genderOfPeople = input("What Gender Are You ") 
#If There Is 2 People In your famly
elif peopleInFamly == 2:
    agesOfPeopleInFamly12 = int(input("How Old Is the first person in your famly "))
    genderOfPeople12 = input("What Gender is the first person in your famly ")
    agesOfPeopleInFamly22 = int(input("How Old Is the Second person in your famly "))
    genderOfPeople = input("What Gender is the Second person in your famly ")
#If There Is 3 People In your famly
elif peopleInFamly == 3:
    agesOfPeopleInFamly13 = int(input("How Old Is the first person in your famly "))
    genderOfPeople = input("What Gender is the first person in your famly ")
    agesOfPeopleInFamly23 = int(input("How Old Is the Second person in your famly "))
    genderOfPeople = input("What Gender is the Second person in your famly ")
    agesOfPeopleInFamly33 = int(input("How Old Is the Third person in your famly "))
    genderOfPeople = input ("What Gender is the Thrid person in your famly ")
You're going to get a headache doing something like this. Instead, you could use parallel lists for each thing:
ages = []
genders = []
peopleInFamly = int(input("How Many People Are In Your Famly "))
for x in range(peopleInFamily):
   age = int(input("How old..."))
   ages.append(age)
   gender = input("What gender...")
   genders.append(genders)
That whole big if/elif block asking for ages and genders goes down to about 10 lines.

also, instead of asking how many weeks then prompting for the dollar amount, how about just ask "how much money do you have?" then ask "How many weeks should that last?" Then do the dollar per week calculation?

You could also simplify things by using dict's instead of lists for defining food items but this is a start.
Ok thanks for all the help but my biggest problem is I do not know how to make the algorithm do you know how that can be done @mpd
(Jan-10-2018, 04:00 AM)TFish Wrote: [ -> ]Ok thanks for all the help but my biggest problem is I do not know how to make the algorithm do you know how that can be done @mpd

That's not an easy algorithm to design because there are a lot of factors to consider. To get started, make simplifying assumptions. For example ignore gender and age and just assume that a person needs X calories of food a day. Write a function that ignores cost and selects foods that provide that many calories. When that works add in some logic to break up those foods over breakfast, lunch, dinner. When that works, consider cost. When that works... etc.

Alternately, start with cost (which really isn't much different than the calories consideration.) Then try writing a function that takes a dollar amount and selects the most food for the given dollar amount.

You can then take these "simple" functions and continue to refine them, add logic for age, gender, etc.
Ok thank you for all the help. I just have one more thing to ask you. I am pretty new to python so this is a ambitious project for me. How would i go about the selection of the cost from the Dictionary. @mpd
(Jan-10-2018, 03:14 PM)TFish Wrote: [ -> ]How would i go about the selection of the cost from the Dictionary.

You can reference items in a dictionary with it's "key" name. In other words, dictionaries map keys to values. So if you wanted to look up people by their age you might have a dictionary such as this:

ages = { "Joe": 32, "Sandy": 16, "Bob": 23, "Janice": 35 }

# you can then get each person's age with their name:
ages["Joe"]
One way to do this with foods, is making a dictionary that maps the names of foods to a dictionary of their details:
# foods is a dict of dict's.  Each food is referenced by it's name...
foods = { "Wheat Bread": { "cost": 2.50, "calories": 300, "servings": 18 },
          "Pepsi": { "cost": 1.00, "calories": 1000, "servings": 1} }

bread = foods["Wheat Bread"]
print("Bread costs {0} per loaf".format(bread["cost"]))

# to run through all foods you can use a for-loop
for name, info in foods.items():
    print("{0} costs {1} dollars".format(name, info["cost"]))
Output:
> python.exe food.py Bread costs 2.5 per loaf Wheat Bread costs 2.5 dollars Pepsi costs 1.0 dollars