Guest User

Untitled

a guest
Oct 22nd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. # U06_Ex03_SphereAreaVol.py
  2. #
  3. # Author: Bill Montana
  4. # Course: Coding for OOP
  5. # Section: A3
  6. # Date: 21 Oct 2017
  7. # IDE: PyCharm Community Edition
  8. #
  9. # Assignment Info
  10. # Exercise: 3
  11. # Source: Python Programming
  12. # Chapter: 6
  13. #
  14. # Program Description
  15. # Solve PE 3.1 using functions sphereArea() and sphereVolume()
  16. # Calculates the volume and surface area of a sphere from its radius,
  17. # given as input.
  18. #
  19. # Algorithm (pseudocode)
  20. # introduce program
  21. # get radius (float) from user
  22. # get units (str) for radius
  23. # call sphereArea() with radius as parameter; assign to area var
  24. # call sphereVolume() with radius as parameter; assign to volume var
  25. # display results
  26. #
  27. # sphereArea()
  28. # r is argument
  29. # calculate and return surface area
  30. # A = 4 * math.pi * r*r
  31. #
  32. # sphereVolume()
  33. # r is argument
  34. # calculate volume and surface area
  35. # V = 4 / 3 * math.pi * r*r*r
  36.  
  37.  
  38.  
  39.  
  40. import math
  41.  
  42. def main():
  43. # introduce program
  44. print("\nThis program calculates the volume and surface area of a sphere.\n")
  45.  
  46. # get radius (float) from user
  47. radius = float(input("What is the sphere's radius? "))
  48.  
  49. # get units (str) for radius
  50. units = str(input("What are the units for the radius? "))
  51.  
  52. # call sphereArea() with radius as parameter; assign to area var
  53. area = sphereArea(radius)
  54.  
  55. # call sphereVolume() with radius as parameter; assign to volume var
  56. volume = sphereVolume(radius)
  57.  
  58. # display results
  59. print(("\nA sphere with radius {0:.3f} " + units + " has a volume of {1:.3f} cubic " + \
  60. units + " and a surface area of {2:.3f} square " + units + ".").format(radius, volume, area))
  61.  
  62. # sphereArea()
  63. # r is argument
  64. def sphereArea(r):
  65. # calculate and return surface area
  66. # A = 4 * math.pi * r*r
  67. area = 4 * math.pi * math.pow(r, 2)
  68. return area
  69.  
  70. # sphereVolume()
  71. # r is argument
  72. def sphereVolume(r):
  73. # calculate volume and surface area
  74. # V = 4 / 3 * math.pi * r*r*r
  75. volume = 4 / 3 * math.pi * math.pow(r, 3)
  76. return volume
  77.  
  78. main()
Add Comment
Please, Sign In to add comment