2.6.2 Sphere Volume
Question: 1. Define a variable suitable for holding the number of bottles in a case.
Answer: bottlePerCase = 6
==================================================
Question: 2. What is wrong with the following statement?
Ounces per liter = 28.35
Answer: There are two errors :
You cannot have spaces in variable names
2. There are about 33.81 ounces per liter, not 28.35
==================================================
Question: 3. Define two variables, unitPrice and quantity, to contain the unit price of a single bottle and a number of bottles purchased. Used reasonable initial values.
Answer: unitPrice = 1.95
quantity = 2
==================================================
Question: 4. Use the variables to Claridon self check three to display the total purchase price.
Answer: print(“Total price: “, unitPrice * quantity
==================================================
Question: 5. Some drinks are sold in four packs instead of six packs. How would you change the volume1.py program to compute the total volume?
Answer: Change the declaration of cansPerPack to cansPerPack = 4
==================================================
Question: 6. Why can’t the variable total volume in the volume1.py program be a constant variable?
Answer: Its value is modified by the assignment statement
==================================================
Question: 7. How could you explain assignment using the parking space analogy?
Answer: Assignment would occur when one car is replaced by another in the parking space.
==================================================
Question: 8. Earns interest once a year. In python how do you commute the interest earned in the first year? Assume variable percent and balance both contain floating point values.
Answer: interest = balance * percent / 100…
==================================================
Question: 9. In python, how do you compute the side length of a square who’s area is stored in the variable area?
Answer: sideLength = sqrt(area)…
==================================================
Question: 10. The volume of the sphere is given by:
V= 4/3pir^3
If the radius is given by variable radius that contains a floating point value, read a python expression for the volume.
Answer: 4 / 3 pi radius ** 3…
==================================================
Question: 11. What is the value of 1729 // 10 and 1729 % 10?
Answer: 172 and 9…
==================================================
Question: 12. If Anna is a positive number, what is (n // 10) % 10?
Answer: Is is the second-to-last digit of n. For example, if n is 1729, then n // 10 is 172, and (n // 10) % 10 is 2…
==================================================
Question: 13. Translate the pseudocode for computing the number of tiles and the gap width into Python.
Answer: pairs = (totalWidth - tileWidth) // (2 * tileWidth)
tiles = 1 + 2 * pairs
gap = (totalWidth - ties * tileWidth) / 2.0…
==================================================
Question: 14. Suppose the architect specifies a pattern with black, grey, white tiles, like this:
B G W G B G W G B
Again, the first and last tile should be black. How do you need to modify the algorithm?
Answer: Now there are groups of four tiles (grey/white/grey/black) following the initial black ties. Therefore, the algorithm is now
number of groups = integer part of (total width - tile width) / (4 x tile width)
number of tiles = 1 + 4 * number of groups…
==================================================
Question: 15. A robot needs to tile a floor with alternating black and white tiles. Develop an algorithm that yields the color (0 for black, 1 for white), given the row and column number. Start with a specific value for the row and column, and then generalize:
2. 3. 4.
1. B W B W
2. W B W B
3. B W B
4. W B
Answer: Clearly, the answer depends only on whether the row and column numbers are even or odd, so let’s first take the reminder after dividing by 2. Then we can enumerate all expected answer
Row %2..Column %2……Color
0…………………0…………………0
0…………………1…………………1
1…………………0…………………1
1…………………1…………………0
==================================================
Question: 16. For a particular car, repair and maintenance costs in year 1 are estimated at $100; in year 10, at $1,500. Assuming that the repair cost increases by the same amount every year, develop pseudocode to compute the repair cost in year 3 and then generalize to year n.
Answer: In nine years, the repair costs increased by $1,400. Therefore, the increase per year is $1,400/9=$156. The repair cost in year 3 would be $100 + 2 $156 = $412. The repair cost in year n is $100 + n $156. To avoid accumulation of roundoff errors, it is actually a good idea to use the original expression that yielded $156, that is,
Repair cost in year n = 100 + n * 1400 / 9
==================================================
Question: 17. The shape of a bottle is approximated by two cylinders of radius r₁ and r₂ and heights of h₁ and h₂, joined by a cone section of height h³.
Using the formulas for the volume of a cylinder, V = pir²h, and a cone section v = pi(r₁² + r₁ r₂ + r₂²) * h³/3,
develop pseudocode to compute the volume of the bottle. Using an actual bottle with know volume as a sample, make a hand calculation of your pseudocode.
Answer: The pseudocode follows easily from the equastions:
bottom volume = pi r₁² h₁
top volume = pi r₂² h₂
middle volume = pi (r₁² + r₁ r₂ + r₂²) * h³ / 3
total volume = bottom volume + top volume + middle volume
Measuring a typical wine bottle yields
r₁ = 3.6, r₂ = 1.2, h₁ = 15, h₂ = 7, h₃ = 6
(all in centimeters). Therefore,
bottom volume = 610.73
top volume = 32.67
middle volume = 778.12
The actual volume is 750 ml, which is close enough to our computation to give confidence that it is correct.
==================================================
Question: 18. What is the length of the string “Python Program”?
Answer: The length is 14. The space counts as a character.
==================================================
Question: 19. Given this string variable, give a method call that returns the string “gram”.
title = “Python Program”
Answer: title.replace(“Python Pro”, ““)
==================================================
Question: 20. Use string concatenation to turn the string variable title = “Python Program” into “Python Programming”.
Answer: title = title + “ming”
==================================================
Question: 21. What does the following statement sequence print?
string = “Harry”
n = len(string)
mystery = string[0] + string[n-1]
print(mystery)
Answer: Hy
==================================================
Question: 22. Write statements to prompt for and read the user’s age.
Answer: age = int(input(“How old are you?”))
==================================================
Question: 23. What is problematic about the following statement sequence?
userInput = input(“Please enter the unit price: “)
unitPrice = int(userInput)
Answer: The second statement calls int, not float. If the user were to enter a price such as 1.95, the program would be terminated with a “value error”.
==================================================
Question: 24. What is problematic about the following statement sequence?
userInput = input(“Please enter the number of cans”)
unitPrice = int(userInput)
Answer: There is no colon and space at the end of the prompt. A dialog would look like this:
Please enter the number of cans6
==================================================
Question: 25. What i s the output of the following statement sequence?
volume = 10
print(“The volume is %5d” % volume)
Answer: The total volume is 10
There are four spaces between is and 10. One space originated from the format string (the space between s and %) and three spaces are added before 10 to achieve the field width of 5.
==================================================
Question: 26. Using the string format operator, print the values of the variables bottles and cans so that the output looks like this:
Bottles: 8
Cans: 24
The numbers to the right should line up. ( You may assume that the numbers are integers and have at most 8 digits.)
Answer: Here is a simple solution:
print(“Bottles: %8d” % bottles)
print(“Cans: %8d” % cans)
Note the spaces after Cans:. Alternatively, you can use format specifiers for the string.
print(“%-8s %8d” % (“Bottles:”, bottles)
print(“%-8s %8d” % (“Cans:”, cans)
==================================================
Question: 27. How do you modify the program
from graphics import GraphicsWindow
Create the window and access the canvas.
win = GraphicsWindow(400,200)
canvas = win.canvas()
Draw on the canvas.
canvas.drawRect(0, 10, 200
Answer: Here is one possible solution:
canvas.drawRect(0,0,50,50)
canvas.drawRect(0,100,50,50)
==================================================
Question: 28. What happens if you call drawOval instead of drawRect in the program of Section 2.6.2?
Answer: The program shows three very elongated ellipses instead of rectangles.
==================================================
Question: 29. Give instructions to draw a circle with center (100,100) and radius 25.
Answer: canvas.drawOval(75,75,50,50)
==================================================
Question: 30. Give instructions to draw a letter “V” by drawing two line segments
Answer: canvas.drawLine(0,0,10,30)
canvas.drawLine(10,30,20,0)
==================================================
Question: 31. Give instructions to draw a string consisting of the letter “V”.
Answer: canvas.drawText(0,30,”V”)
==================================================
Question: 32. How do you draw a yellow square on a red background?
Answer: win = GraphicsWindow(200,200)
canvas = win.canvas()
canvas.setColor(“red”)
canvas.drawRect(0,0,200,200)
canvas.setColor(“yellow”)
canvas.drawRect(50,50,100,100)
==================================================