Starting out with python 3rd edition Download Book Starting Out With Python 3rd Edition in PDF format. You can Read Online Starting Out With Python 3rd Edition here in PDF, EPUB, Mobi or Docx formats. Programming Exercise 2-1
('Jack Shepard') ('123 Lost Lane') ('Boston, MA 99999') ('704-555-1212') ('Computer Science')
Start
Display “Jack Shepard”
Display “123 Lost Lane”
Display “Boston, MA 99999”
Display “704555-1212”
Display “Computer Science”
End
Programming Exercise 2-2 # Variables to hold the sales total and the profit salesTotal = 0.0 profit = 0.0 # Get the amount of projected sales. salesTotal = float(input('Enter the projected sales: ')) # Calculate the projected profit. profit = salesTotal * 0.23 # Print the projected profit. print ('The projected profit is ', format(profit, '.2f'))
Start
A
Declare Real salesTotal, profit
Set profit = salesTotal * 0.23
Display “Enter the projected sales.”
Input salesTotal
A
Display “The projected profit is “, profit
End
Programming Exercise 2-3 # Variables to hold the size of the tract and number of acres. tractSize = 0.0 acres = 0.0 # Constant for the number of square feet in an acre. SQ_FEET_PER_ACRE = 43560 # Get the square feet in the tract. tractSize = input('Enter the number of square feet in the tract.') # Calculate the acreage. acres = float(tractSize) / SQ_FEET_PER_ACRE # Print the number of acres. print ('The size of that tract is', format(acres, '.2f'), 'acres.')
Start
A
Declare Real tractSize, acres
Input tractSize
Constant Integer SQ_FEET_PER_ACRE = 43560
Set acres = tractSize / SQ_FEET_PER_ACRE
Display “Enter the number of square feet in the tract.”
Display “The size of that tract is“, acres, “acres.”
A
End
Programming Exercise 2-4 # Variables to hold the prices of each item, the subtotal, # and the total. item1 = 0.0 item2 = 0.0 item3 = 0.0 item4 = 0.0 item5 = 0.0 subtotal = 0.0 tax = 0.0 total = 0.0 # Constant for the sales tax rate. TAX_RATE = 0.07 # Get item1 item2 item3 item4 item5
the price of each item. = float(input('Enter the = float(input('Enter the = float(input('Enter the = float(input('Enter the = float(input('Enter the
price price price price price
of of of of of
item item item item item
#1: #2: #3: #4: #5:
# Calculate the subtotal. subtotal = item1 + item2 + item3 + item4 + item5 # Calculate the sales tax. tax = subtotal * TAX_RATE # Calculate the total. total = subtotal + tax # Print the values. print ('Subtotal: ', format(subtotal, '.2f')) print ('Sales Tax: ', format(tax, '.2f')) print ('Total: ', format(total, '.2f'))
')) ')) ')) ')) '))
Start
Declare Real item1, item2, item3, item4, item5, subtotal, tax, total
A
Display “Enter the price of item #3.”
B
Set subtotal = item1 + item2 + item3 + item4 + item5
Constant Real TAX_RATE = 0.07
Input item3
Display “Enter the price of item #1.”
Display “Enter the price of item #4.”
Set total = subtotal + tax
Input item1
Input item4
Display “Subtotal: \$”, subtotal
Display “Enter the price of item #2.”
Display “Enter the price of item #5.”
Display “Sales Tax: \$”, tax
Input item2
Input item5
Display “Total: \$”, total
A
B
End
Set tax = subtotal * TAX_RATE
Programming Exercise 2-5 # Variables to hold the distances. distance6Hours = 0.0 distance10Hours = 0.0 distance15Hours = 0.0 # Constant for the speed. SPEED = 70 # Calculate the distance the car will travel in # 6, 10, and 15 hours. distance6Hours = SPEED * 6 distance10Hours = SPEED * 10 distance15Hours = SPEED * 15 # Print the results. print ('The car will travel the following distances:') print (distance6Hours,'miles in 6 hours.') print (distance10Hours,'miles in 10 hours.') print (distance15Hours,'miles in 15 hours.')
Start
A
Declare Real distance6Hours, distance10Hours, distance15Hours
Display “The car will travel the following distances.”
Constant Real SPEED = 70
Display distance6Hours, “miles in 6 hours.”
Set distance6Hours = SPEED * 6
Set distance10Hours = SPEED * 10
Set distance15Hours = SPEED * 15
A
Display distance10Hours , “miles in 10 hours.”
Display distance15Hours , “miles in 15 hours.”
End
Programming Exercise 2-6 # Variable declarations purchase = 0.0 stateTax = 0.0 countyTax = 0.0 totalTax = 0.0 totalSale = 0.0 # Constants for the state and county tax rates STATE_TAX_RATE = 0.05 COUNTY_TAX_RATE = 0.025 # Get the amount of the purchase. purchase = float(input('Enter the amount of the purchase: ')) # Calculate the state sales tax. stateTax = purchase * STATE_TAX_RATE # Calculate the county sales tax. countyTax = purchase * COUNTY_TAX_RATE # Calculate the total tax. totalTax = stateTax + countyTax # Calculate the total of the sale. totalSale = purchase + totalTax # Print information about the sale. print ('Purchase Amount:', format(purchase, '.2f')) print ('State Tax:', format(stateTax, '.2f')) print ('County Tax:', format(countyTax, '.2f')) print ('Total Tax:', format(totalTax, '.2f')) print ('Sale Total:', format(totalSale, '.2f'))
Start
A
B
Declare Real purchase, stateTax, countyTax, totalTax, totalSale
Set countyTax = purchase * COUNTY_TAX_RATE
Display “Total Tax: “, totalTax
Constant Real STATE_TAX_RATE = 0.05
Set totalTax = stateTax + countyTax
Display “Sale Total: “, totalSale
Constant Real COUNTY_TAX_RATE = 0.025
Display “Enter the amount of the purchase.”
Set totalSale = purchase + totalTax
Display “Purchase Amount: “, purchase
Display “State Tax: “, stateTax Input purchase Display “County Tax: “, countyTax Set stateTax = purchase * STATE_TAX_RATE
A
B
End
Programming Exercise 2-7 # Declare variables to hold miles driven, gallons # of fuel used, and miles-per-gallon. miles = 0.0 gallons = 0.0 mpg = 0.0 # Get the miles driven. miles = float(input('Enter the miles driven: ')) # Get the gallons of fuel used. gallons = float(input('Enter the gallons of fuel used: ')) # Calculate miles-per-gallon. mpg = miles / gallons # Print the result. print ('You used', format(mpg, '.2f'), 'miles per gallon.')
Start
A
Declare Real miles, gallons, mpg
Input gallons
Display “Enter the miles driven.”
Set mpg = miles / gallons
Input miles
Display “Enter the gallons of fuel used.”
A
Display “You used”, mpg “miles per gallon.”
End
Programming Exercise 2-8 # Declare variables for food charges, tip, tax, and total. food = 0.0 tip = 0.0 tax = 0.0 total = 0.0 # Constants for the tax rate and tip rate. TAX_RATE = 0.07 TIP_RATE = 0.18 # Get the food charges. food = float(input('Enter the charge for food: ')) # Calculate the tip. tip = food * TIP_RATE # Calculate the tax. tax = food * TAX_RATE # Calculate the total. total = food + tip + tax # Print the tip, tax, and total. print ('Tip: \$', format(tip, '.2f')) print ('Tax: \$', format(tax, '.2f')) print ('Total: \$', format(total, '.2f'))
Start
A
Declare Real food, tax, tip, total
Set tax = food * TAX_RATE
Constant Real TAX_RATE = 0.07
Set total = food + tip + tax
Constant Real TIP_RATE = 0.18
Display “Tip: \$”, tip
Display “Enter the charge for food.”
Display “Tax: \$”, tax
Input food
Display “Total: \$”, total
Set tip = food * TIP_RATE
End
A
Programming Exercise 2-9 # Declare variables to hold the temperatures. celsius = 0.0 fahrenheit = 0.0 # Get the Celsius temperature. celsius = float(input('Enter a Celsius temperature: ')) # Calculate the Fahrenheit equivalent. fahrenheit = (9.0 / 5.0) * celsius + 32 # Display the Fahrenheit temperature. print ('That is equal to', format(fahrenheit, '.2f'), 'degrees Fahrenheit.')
Start
Declare Real celsius, fahrenheit
Display “Enter a Celsius temperature.”
Input celsius
Set fahrenheit = (9.0 / 5.0) * celsius + 32
Display “That is equal to”, fahrenheit, “degrees Fahrenheit
End
Start
A
Declare Real cookies, sugar, butter, flour
Constant Real SUGAR_RECIPE = 1.5
Constant Real BUTTER_RECIPE = 1.0
Constant Real FLOUR_RECIPE = 2.75
Display “Enter the number of cookies.”
A
B
Display butter, “cups of butter.”
Display flour, “cups of flour.”
Display sugar, “cups of sugar.”
B
Programming Exercise 2-11 # Variables for the number of male and female students, # total number of students, and the percentage of male # and female students. male = 0 female = 0 total = 0 percentMale = 0.0 percentFemale = 0.0 # Get the number of male students. male = int(input('Enter the number of male students: ')) # Get the number of female students. female = int(input('Enter the number of female students: ')) # Calculate the total number of students. total = male + female # Calculate the percentage of male students. percentMale = male / total # Calculate the percentage of female students. percentFemale = female / total # Print the percentage of male students. print('Male:', format(percentMale, '.2f'), '%') # Print the percentage of female students. print('Female:', format(percentFemale, '.2f'), '%')
Start
A
Declare Integer male, female, total
percentMale = male / total
Declare Real percentMale, percentFemale
percentFemale = female / total
Input male
Display “Male:”, percentMale, “%:”
Input female Display “Female:”, percentFemale, “%” total = male + female
End A
Programming Exercise 2-12 # Named constants COMMISSION_RATE = 0.03 NUM_SHARES = 2000 PURCHASE_PRICE = 40.0 SELLING_PRICE = 42.75 # Variables amountPaidForStock = 0.0 purchaseCommission = 0.0 totalPaid = 0.0 stockSoldFor = 0.0 sellingCommission = 0.0 totalReceived = 0.0 profitOrLoss = 0.0
# # # # # # #
Amount paid for the stock Commission paid to purchase stock Total amount paid Amount stock sold for Commission paid to sell stock Total amount received Amount of profit or loss
# Calculate the amount that Joe paid for the stock, not # including the commission. amountPaidForStock = NUM_SHARES * PURCHASE_PRICE # Calculate the amount of commission that Joe paid his broker # when he bought the stock. purchaseCommission = COMMISSION_RATE * amountPaidForStock # Calculate the total amount that Joe paid, which is the amount # he paid for the stock plus the commission he paid his broker. totalPaid = amountPaidForStock + purchaseCommission # Calcualate the amount that Joe sold the stock for. stockSoldFor = NUM_SHARES * SELLING_PRICE # Calculate the amount of commission that Joe paid his broker # when he sold the stock. sellingCommission = COMMISSION_RATE * stockSoldFor # Calculate the amount of money left over, after Joe paid # his broker. totalReceived = stockSoldFor - sellingCommission # Calculate the amount of profit or loss. If this amount is a # positive number, it is profit. If this is a negative number it # is a loss. profitOrLoss = totalReceived – totalPaid # Print the required data. print ('Amount paid for the stock: \$', format(amountPaidForStock, '.2f')) print ('Commission paid on the purchase: \$', format(purchaseCommission, '.2f')) print ('Amount the stock sold for: \$', format(stockSoldFor, '.2f')) print ('Commission paid on the sale: \$', format(sellingCommission, '.2f')) print ('Profit (or loss if negative): \$', format(profitOrLoss, '.2f'))
Start
A
B
Constant Real COMMISSION_RATE = 0.03 Constant Integer NUM_SHARES = 2000 Constant Real PURCHASE_PRICE = 40.0 Constant Real SELLING_PRICE = 42.75
Set totalPaid = amountPaidForStock + purchaseCommission
Display “Amount paid for stock: \$”, amountPaidFor Stock
Declare Real amountPaidForStock Declare Real purchaseCommission Declare Real totalPaid Declare Real stockSoldFor Declare Real sellingCommission Declare Real totalReceived Declare Real profitOrLoss
Set sellingCommission = COMMISSION_RATE * stockSoldFor
Set stockSoldFor = NUM_SHARES * SELLING_PRICE
Set amountPaidForStock = NUM_SHARES * PURCHASE_PRICE
Set profitOrLoss = totalReceived - totalPaid
Set purchaseCommission = COMMISSION_RATE * amountPaidForStock
B
A
Display “Commission paid on the purchase: \$”, purchaseComm ission
Display “Amount the stock sold for: \$”, stockSoldFor
Display “Commission paid on the sale: \$”, sellingCommissi on
Display “Profit (or loss if negative): \$, profitOrLoss
End