I2P- Winter break- Codeacademy

Throughout the holiday we were challenged to continue coding through codeacademy. The aim of this was to continue to code, and to not lose the language throughout our holiday. At first, I found the codeacademy quite repetitive, but after a while, I managed to incorporate five minutes into each day of the break. Some of the code was different from what we used in class, but overall the codeacademy was a good refresher to what we were already taught within the coding class.

A day at the supermarket (sublesson):

This subsection talked mostly about how to use lists and dictionaries:

BeFOR We Begin:

For loops:

For loops are used in order iterate through all of the elements in a list from the left-most to the right-most.

for item in [1, 3, 21]: 
    print item

A variable, in this case “item”, can be a variable between for and in. Remember that within the brackets to put perhaps the list you want to print from. For example if the list is named names= [Adam, Benny, Catherine] that in the for loop you make a new variable (eg. item) and you want to print all of the items in the list. Your code will look like this:

for items in [names]:
print items

This is KEY!:
 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."
 }
# Add your code below!
for key in webster:
 print webster[key]

For this code I had to print the definition of each item in the “dictionary”. This meant creating the variable key and for key in webster, we would print an amount or a certain part of the dictionary.

List and Functions: 

For this task, the program asked for the me to code a function that counted the number of times the word fizz is found within a list:

def fizz_count(x):
 count = 0
 for item in x:
 if item=="fizz":
 count= count+1
 return count
print fizz_count(["fizz", "buzz", "fizz"])

The code essentially is a function that takes from the list “x”. The function counts how many times the string “fizz” is in the list. Since the aim of the code is to create a count, the count variable is used and for every item that equals “fizz”, will make the counter go up by one.

Your own store: 

Create a inventory of the different items that you keep at your store:

prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}
stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15
}

Something of value: 

total = 0
for fruit in stock:
 total = total +(stock[fruit] * prices[fruit])
print total

For this section I had to create a code that multiplied the stocks of fruit, by the prices of the fruit.  In order to do this we started off with the stock of each fruit. For the fruits in stock, the total would be equal to the total added to the fruit stock number with the prices of the fruit.

Making a purchase: 

For each number in the list, we add that number to the running sum of the total. In order to make a code that takes both the lists and dictionaries; combines them and makes a price into count, we had to use the for in loop.

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 item in food: 
total += prices[item]
return total 
def compute_bill(food): 
total= 0
for item in food:
if stock [item] > 0: 
total += prices[item]
stock[item]= stock[item]- 1 
return total

For this part in the code, we had to create a program and modify the program that we already made. For this part we only did the for loop if the item was in stock. If it was in stock, so when it equaled to more than 0, then the total would equal to the price of the item. It would also mean that the stock of the item was less, which would equate in the item stock being one less.

Student becomes master: 

lloyd = {
"name": "Lloyd",
"homework": [],
"quizzes": [],
"tests": []
}
alice = {
"name": "Alice",
"homework": [],
"quizzes": [],
"tests": []
}
tyler = {
"name": "Tyler",
"homework": [],
"quizzes": [],
"tests": []
}

In this section, the lesson  is to try to code everything with the previous knowledge that you already have had.

 

 

Leave a Reply