A day at the supermarket

For the assignment “A day at the supermarket” in CodeAcademy, the main aspect in the first part of the exercise is to refresh our memory on how to use a for loop. For me, it was a bit confusing because I had trouble using for loops in the past. However, a for loop just makes it simple for you to apply  a certain action to a bunch of different items within a list. In the case of the first slide, we needed to print out all of the names in a list and as you can tell from the code, it’d be a lot shorter than printing all of the names out individually. The code would be as follows:

names = [“Adam”,”Alex”,”Mariah”,”Martine”,”Columbus”]
for names in names:
print names

 

a-day-at-the-supermarket1

The second step of the exercise, went over the fact that you can loop through the keys in a dictionary by using a for loop. However, because dictionaries are usually not in order, they won’t give you the exact same order every time. Using a for loop here, would be another great example of the time you can save while using a for loop. The following is the code for looping through the keys of a dictionary:

webster = {
“Aardvark” : “A star of a popular children’s cartoon show.”,
“Baa” : “The sound a goat makes.”,
“Carpet”: “Goes on the floor.”,
“Dab”: “A small amount.”
}

for key in webster:
print webster[key]

supermarlet2

For the third part of the exercise, the lesson demonstrates that if you wanted to find specific numbers within a list, you can  also do that with a for loop. In this case we needed to print out number that were even. This part was a bit challenging until I realized there was another way to communicate the meaning of even, which was basically to check whether or not the number is divisible by two. After that is checked, then you can simply print out the numbers you need. This is demonstrated in the following code:

a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
for a in a:
if a % 2 ==0:
print a

The next step of the exercise demonstrates that if you’d like, you can use a function to count how many times a word appears in a list. This can be especially helpful if you are dealing with a list that has repetitive values.  We needed to first create a function called fizz_count that takes a list x as input. Then we needed to create a variable we’d name count to hold the ongoing count. by initializing it to zero. Then we needed to check which values equaled to fizz and from there the count would increase by one, the same way it does when you count things by hand. This is demonstrated in the following code:

def fizz_count(x):
count = 0
for y in x:
if y == “fizz”:
count = count + 1
return count

fizz_count([“fizz”,”buzz”,”fizz”])

supermarket-3

The next step was merely to view the string iteration example CodeAcedemy provided.

The next step of the exercise was to create a new dictionary called prices and to essentially assign meanings to the keys provided  as demonstrated in the following code:

prices = {
“banana”: 4,
“apple”: 2,
“orange”: 1.5,
“pear”: 3
}

supermarket4

Afterwards, the next step was to pretend as if though we were store owners who needed to create a stock dictionary for our inventory. Essentially we were to assign numeric values to items that would normally be in a store within a small dictionary. The following code for this dictionary is as follows:
prices = {
“banana”: 4,
“apple”: 2,
“orange”: 1.5,
“pear”: 3
}

stock = {
“banana”: 6,
“apple”: 0,
“orange”: 32,
“pear”: 15
}

 

Afterwards, we needed to print out all of our information from different dictionaries. Since we both dictionaries have the  same keys, we can loop through one dictionary and prinvalues from both of them at once. This particular step gave me a lot of trouble for the reason that I misspelled a specific word and couldn’t quite get what I got wrong, but after that things ran smoothly. This is the code for this particular step:

prices = {
‘banana’ : 4,
‘apple’ : 2,
‘orange’ : 1.5,
‘pear’ : 3
}
stock = {
‘banana’ : 6,
‘apple’ : 0,
‘orange’ : 32,
‘pear’ : 15
}
for fruit in prices:
print fruit
print “price: %s” % prices[fruit]
print “stock: %s” % stock[fruit]

supermarket

The next step required us to find out how much money would be made if we spent all of our inventory. We would first have to create a new variable called total and set it to zero. Next we would have to loop through the prices dictionaries. For all keys in the dictionaries, we would need to multiply the number in prices by the number in stock, the way you would normally. we would the need to add that value to the total. Afterwards, we would need to print. The code is as follows:
prices = {
“banana”: 4,
“apple”: 2,
“orange”: 1.5,
“pear”: 3
}

stock = {
“banana”: 6,
“apple”: 0,
“orange”: 32,
“pear”: 15
}

total = 0
for item in prices:
total += prices[item] * stock[item]
print total

The next step was to essentially make a simple list. Which gave us a quick refresher on how to write a list. The code is as follows:

groceries = [‘banana’,’orange’,’apple’]

The next step would essentially help us determine how much we would need to pay for all of the items on our grocery list. We would  first need to define the function sum, giving it an argument called numbers. We would then initialize the variable total. Afterwards, we would need to, for every number within the list, add that number to total. We would need to return the sum. Afterwards, we would need to create a list of numbers called n. Lastly, we call the function with the variable called n and print it. The code is as follows:

shopping_list = [“banana”, “orange”, “apple”]

stock = {
“banana”: 6,
“apple”: 0,
“orange”: 32,
“pear”: 15
}

prices = {
“banana”: 4,
“apple”: 2,
“orange”: 1.5,
“pear”: 3
}

# Write your code below!
def compute_bill(food):
total = 0
for x in food:
total += prices[x]
return total

In the next step we would essentially just be making changes in order for us to take inventory for certain items. This is important because if something isn’t available, it shouldn’t be include in the total. The changes we had to make were the following : when looping through each item of food, we would only add the price of the item total if the stock number is more than zero. After that, if the number was in stock and we added the price to the total, we would subtract one from the stock count of the item. The code is as follows:

hopping_list = [“banana”, “orange”, “apple”]

stock = {
“banana”: 6,
“apple”: 0,
“orange”: 32,
“pear”: 15
}

prices = {
“banana”: 4,
“apple”: 2,
“orange”: 1.5,
“pear”: 3
}

# Write your code below!
def compute_bill(food):
total = 0
for x in food:
total += prices[x]
return total

final-supermarket