|
CMPT 120: Lab 6
Write a program that computes the cosecant of an angle entered by the user. Remember that cosecant(x) is equal to 1/sin(x). The sin function is available in the math module.
The sin of an angle can be zero (for example when the angle is 0) so this calculation could cause a division by zero error. Instead of using a simple if statement to catch this possibility, use a try-except structure to catch this error. If it occurs, print out a message, otherwise output the computed cosecant.
Enter an angle: 1
The cosecant of that angle is 1.18839510578.
Enter an angle: 0
The cosecant of that angle is undefined.
Write a function squares(n) that returns a list of all the perfect squares from 1 to n2. So squares(5) should return [1,4,9,16,25] .
Create a function even_only that takes a list as an argument. It should return a list that consists of all the elements from the argument that are even numbers. For example, even_only([2,4,5,9,10,12]) should return [2, 4, 10, 12] ; even_only(squares(10)) should return [4, 16, 36, 64, 100] .
You should be familiar with it by now, but remember that the modulus operator % returns the remainder of the first operand divided by the second. For example 5 % 3 returns 2. This operator can be used to test whether or not a number is even.
Chris Schmidt, last updated June 19, 2007 |