A first function for triangles
Q1. Type this code into Python and get it working:
1 #Calculate the area of a triangle from the base and height
2
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
8
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.
12 print(triangle)
Q2. What does this program do?
Q3. Line 1 is a comment. What do single line comments always begin with?
Q4. How do you easily make multi-line comments? Where is there an example of a multi-lne comment in the program.
Q5. What is meant by a function's 'docstring'?
Q6. The word 'argument' has been used a couple of times in the comments. What is an 'argument'?
Q7. Line 3 uses the word 'parameter' in the comment. What is a 'parameter'?
Q8. Parameters and arguments are frequently used incorrectly. What is meant by this statement?
Q9. What data type is a 'float'? Give a couple of examples.
Q10. Explain in detail how lines 9 - 11 work.
Q11. Explain how lines 7 and 12 work.
Q12. Write a new program that asks the user for the three sides of a triangle and returns the length of the perimeter.