More functions for triangles
Q1. Get this code working in Python:
1 #Calculate the area of a triangle from the base and height
3 def area_bh(base, height): # This function has two parameters.
4 '''Calculates the area of a triange from the base and height.
5 Requires two float arguments to work and returns a single float.'''
6 area = 0.5 * base * height
7 return area #This is the answer 'returned' from the function
9 myBase = float(input('Please enter the base >>> ')) #get the base
10 myHeight = float(input('Please enter the height >>> ')) #get the height
11 triangle = area_bh(myBase, myHeight) #call the function area_bh and pass it two arguments.
Q2. Describe what this program does. We have met it before in another worksheet.
Q3. Using the Internet, find out what Heron's formula calculates.
Q4. When was Heron's formula discovered approximately and by whom?
Q5. Write down the steps involved in calculating Heron's formula.
Q6. To use the square root function in Python you need to install the math module. You can see how we did this in the above program, although we haven't used any of the functions in the math module yet. Notice that the math module begins with a small letter and is math not maths. Do some research. Find out what functions are available in the math module.
Q7. To use the square root function in the math module, you state the name of the module, followed by a dot, followed by the function name and followed by any data. To find the square root of e.g. 220, you would do this: math.sqrt(220) Write down how you would find the square root of 300 and store it in a variable called answer.
Q8. Write a program that asks the user to enter in three numbers and then returns both the perimeter of the triangle and the area. HINT: There are a number of ways of doing this. Since you already have a function that calculates the perimeter, perhaps one way is to write a new function that expects four parameters: the first side, the second side, the third side and the perimeter.
Q9. Find a method using the Internet that allows you to round the area to three decimal places.
Q10. Check your answers with these values:
side 1 = 5, side 2 = 11 and side 3 = 12 (the perimeter is 28.0 and the area is 27.495)
side 1 = 10, side 2 = 22 and side 3 = 24 (the perimeter is 56.0 and the area is 109.982)
Make sure you ask your user for the units they are using, and then print out the correct units with the answer.