Comments

CS 65: Introduction to Computer Science I

Programming for humans

Comments: elements of a program which are ignored by the interpreter

  • help document how to use the code properly
  • explain what's going on with more complex code
  • cite references
  • temporarily ignore some code
  • anything for the sake of humans (you and others)

# starts a comment - everything on the rest of the line is ignored

In [ ]:
avg_gas_estimate = 2.70  #current price per gallon in Des Moines
round_trip_miles_to_memphis = 1224
vehicle_mpg = 32
gas_cost = avg_gas_estimate * (round_trip_miles_to_memphis/vehicle_mpg)

Multiline comments

Text surrounded by three sets of quotes will all be ignored - spanning multiple lines

In [ ]:
"""
Here are some estimates used to help calculate
the budget for our vacation to Memphis next
spring break.
"""
avg_gas_estimate = 2.70 #current price in Des Moines
round_trip_miles_to_memphis = 1224
vehicle_mpg = 32
gas_cost = avg_gas_estimate * (round_trip_miles_to_memphis/vehicle_mpg)

Common uses

In [ ]:
avg_gas_estimate = 2.70 #current price in Des Moines
round_trip_miles_to_memphis = 1224
vehicle_mpg = 32
gas_cost = avg_gas_estimate * (round_trip_miles_to_memphis/vehicle_mpg)

vacation_days = 8

#camping_nights = 3
#hotel_nights = vacation_days-1-camping_nights
hotel_nights = vacation_days-1
hotel_per_night_cost = 96 #Airbnb near Graceland
lodging_cost = hotel_nights * hotel_per_night_cost

food_per_person_per_day = 40
num_people = 4
total_food_per_day = food_per_person_per_day*num_people
souvenirs_and_incidentals_per_day = 100
food_and_incidental_cost = vacation_days*(total_food_per_day+souvenirs_and_incidentals_per_day)

#TODO: look up cost for a Grizzlies game and see if we can afford it

#https://www.graceland.com/ticket-information
graceland_ticket_cost = 190
graceland_cost = num_people * graceland_ticket_cost,

vacation_budget = gas_cost + lodging_cost + food_and_incidental_cost + graceland_cost
print("Vacation budget:",vacation_budget)