# Taking a Vacation

The first step in this assignment was to create a function called answer, that takes no arguments, and returns the value of 42. In order to do this, I copied the format of the previous functions. Typed out, the function is as follows:

“def answer():

return 42”

The second step, was to define another function, called hotel_cost, which takes the argument nights, and basically communicates that a stay at a hotel would cost \$140 per night. It looks like this:

“def hotel_cost(nights):
return 140 * nights”

The third step, which was the most confusing, instructed you to first define a function called “plane_ride_cost” that takes the argument “city” and returns different prices, depending on the city, afterwards you needed to create if statements which named the different cities and how much they would cost. The code was as follows:

“def hotel_cost(nights):
return 140 * nights
def plane_ride_cost(city):
if city ==”Charlotte”:
return 183
elif city ==”Tampa”:
return 220
elif city ==”Pittsburgh”:
return 222
elif city ==”Los Angeles”:
return 475″

In the next step, we would need to define a function called rental_car_cost with an argument called days. Then calculate the cost with the following conditions:

Every day you rent the car costs \$40.

If you rent the car for 7 or more days, you get \$50 off your total.

if you rent the car for 3 or more days, you get \$20 off your total.

You cannot get both of the above discounts.

Afterwards we would return the cost. The code is as follows:

def hotel_cost (nights):
return 140 * nights
def plane_ride_cost (city):
if city == “Charlotte”:
return 183
elif city == “Tampa”:
return 220
elif city == “Pittsburgh”:
return 222
elif city == “Los Angeles”:
return 475
def rental_car_cost (days):
if days < 3:
return 40 * days
elif days > 6:
return 40 * days – 50
else:
return 40 * days – 20 The next step would essentially help us determine how much the total cost of our trip would add up to. We would need to define a function called trip_cost that takes two arguments, city and days, the function  would then return the sum of calling the `rental_car_cost(days)`, `hotel_cost(days)`, and `plane_ride_cost(city)` functions.

The code is as follows:

def hotel_cost (nights):
return 140 * nights
def plane_ride_cost (city):
if city == “Charlotte”:
return 183
elif city == “Tampa”:
return 220
elif city == “Pittsburgh”:
return 222
elif city == “Los Angeles”:
return 475
def rental_car_cost (days):
if days < 3:
return 40 * days
elif days > 6:
return 40 * days – 50
else:
return 40 * days – 20
def trip_cost (city, days):
return rental_car_cost(days) + hotel_cost (days) + plane_ride_cost (city)
In the next step, we’d simply be changing the function called trip_cost by adding a third argument called spending_money. The next thing would be to add the variable spending_money to the sum it returns. The code is as follows:

def hotel_cost (nights):
return 140 * nights
def plane_ride_cost (city):
if city == “Charlotte”:
return 183
elif city == “Tampa”:
return 220
elif city == “Pittsburgh”:
return 222
elif city == “Los Angeles”:
return 475
def rental_car_cost (days):
if days < 3:
return 40 * days
elif days > 6:
return 40 * days – 50
else:
return 40 * days – 20 The final step was to simply after the code from the last step, print the `trip_cost(` to `"Los Angeles"` for 5 days with an extra 600 dollars of spending money. The code is as follows:

def hotel_cost (nights):
return 140 * nights
def plane_ride_cost (city):
if city == “Charlotte”:
return 183
elif city == “Tampa”:
return 220
elif city == “Pittsburgh”:
return 222
elif city == “Los Angeles”:
return 475
def rental_car_cost (days):
if days < 3:
return 40 * days
elif days > 6:
return 40 * days – 50
else:
return 40 * days – 20
def trip_cost (city, days, spending_money):
return rental_car_cost(days) + hotel_cost (days) + plane_ride_cost (city) + spending_money

print trip_cost (“Los Angeles”, 5, 600)

# 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 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] 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”]) 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
} 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 prin`t `values 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] 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 # Date and Time

The most recent lab we needed to complete was called ” Date and Time.” In this lab, we were able to learn how to extract different aspects of time without needing to extract all of them, for instance, if you needed to mention the time, you don’t need to mention the year and month.

The point of the exercise was to essentially extract the year, month and day of today by first importing datetime and later merely putting a print statement in front of what you wanted to extract.

Afterwards, we needed to get more specific, this meant we needed to put a separate print statement for year, month, and day.  Depending on what you desired from the time, according to the third part of the exercise, we needed to extract all three, the code is as follows:

from datetime import datetime

now = datetime.now()
print now

print now.year
print now.month
print now.day The second part of the exercise taught us how to extract a certain date in order for you to either format it in month/ day/ year or year/ month/ day. Firstly, we needed to import datetime

then print ‘%s/%s/%s’ % (now.month, now.day, now.year)

the %s are essentially place holders for the information in the parenthesis and the / character in between them can easily be changed into a – character depending on your own preference.

You can either present the date like this 2016-11-28 or 11/ 28 /2016.

The code for this part of the exercise is as follows:

from datetime import datetime
now = datetime.now()

print ‘%s-%s-%s’ % (now.year, now.month, now.day)
print ‘%s/%s/%s’ % (now.month, now.day, now.year) The next part of the exercise demonstrated that in between characters you can also have the : character, and you could get more specific and extract the time now by extracting the hours minutes and seconds. I followed the same steps as before, only this time merely substituting the month, day, and year for hours, minutes, and seconds. The code is as follows:

from datetime import datetime
now = datetime.now()

print ‘%s-%s-%s’ % (now.year, now.month, now.day)
print ‘%s/%s/%s’ % (now.month, now.day, now.year)
print ‘%s:%s:%s’ % (now.hour, now.minute, now.second) The last part of the exercise ensured that we knew how to put multiple forms of time within a print statement, meaning more than three different types, which involved a different approach where we needed to add more placeholders, and be more aware of the placement of what we needed to print out. In this case I essentially needed to combine two different lines of code into one print statement by simply adding the placeholders together, and the information within the parenthesis together, but making sure that they were in the exact order in which they needed to be. The code is as follows:

from datetime import datetime
now = datetime.now()

print ‘%s-%s-%s’ % (now.year, now.month, now.day)
print ‘%s/%s/%s’ % (now.month, now.day, now.year)
print ‘%s:%s:%s’ % (now.hour, now.minute, now.second)
print ‘%s/%s/%s %s:%s:%s’ % (now.month, now.day, now.year,
now.hour, now.minute, now.second) # Tip Calculator

Essentially for our first homework, we were to complete the assignment called “Tip Calculator” from www.Codeacademy.com. The first step of the assignment allowed us to practice creating variables in python. We were instructed to create a variable for the amount of tax on a meal, and in order to do that we had to write out the name of the variable, (tax) and set it equal to the decimal value of a percentage given (6.75%). Therefore, in order to create a variable, we had to write out the following tax = .0675. While doing this, I had to keep in mind I had to convert the percentage into a decimal. After creating the variable for tax, we needed to compute how much a tip of 15% would be if the meal costed \$44.50 and the tax was 6.75% (the previous variable we created). However, before we did this computation, we would need to create another variable, label it as tip, and set it to the decimal value of 15%. In order to create this variable, we would need to do the same thing we did for creating the variable tax, which was to type tip = .15. One thing during this step that confused me was having to realize that “=” did not mean equal to, but it meant assigning a value to  a variable. The third step of this process was to reassign the value of the variable “meal” in order to calculate the entire price of the meal, including tax . In order to do this, we needed to type out, “meal = meal+meal*tax” this part of the assignment allowed us to call a variable in the same variable while also changing it’s value. In the next step, we were instructed to create a new variable called “total” where we would need to set it equal to the value of “meal+meal*tip” this would look like total = meal + meal *tip. This would basically give us the total cost of a meal, including tax and tip.

# Welcome!

This is the first post on your Learning Blog. Edit or delete it, then start blogging!

The ePortfolio is both a Learning Blog and an Academic Career Portfolio. Use the Learning Blog to document your learning experiences and class assignments each semester. As time goes by, add content to the Academics and Career sections to show your department, graduate institutions, or future employers how well prepared you are for your chosen career.

NOTE: Remember to add appropriate Categories and Tags to your posts. This will help your professors and other visitors find the content they are looking for. The Categories “Coursework” and “Field Trips” and the Tags “OpenLab” and “City Tech” have already been applied to this post. Feel free to make changes!