Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Grocery List Program
#1
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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#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,  ")
 
 
     
Reply
#2
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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:
1
2
3
4
5
6
7
8
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.
Reply
#3
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
Reply
#4
(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.
Reply
#5
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
Reply
#6
(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:

1
2
3
4
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:
1
2
3
4
5
6
7
8
9
10
# 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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Program to find Mode of a list PythonBoy 6 2,607 Sep-12-2023, 09:31 AM
Last Post: PythonBoy
  Create a program that PING a list of IPs skaailet 7 10,167 Mar-26-2020, 10:46 PM
Last Post: snippsat
  how to open program file .exe from list SayHiii 2 3,285 Dec-11-2019, 01:28 AM
Last Post: SayHiii
  creating a list during program execution. hobbyprogrammer 2 3,097 Jan-26-2019, 09:06 PM
Last Post: hobbyprogrammer

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020