Practice 1 (Year 2 Sem 1)

Practice 1 is the first practice in semester 1 of year 2, and the first practice in the total of year 2. It is a practice for modules, and is considered to be easy.

Description
No description was provided for this practice.

Prerequisites

 * Complete Lab 1: Introduction to Python
 * Complete Lab 2: Python Fundamentals
 * Complete Lab 3: Modules

Unlocked after completion

 * Practice makes perfect! (Score at least 60% for Practice 1 [Chapter 3])
 * Intern of the Year (Score at least 60% for Practice 1 [Chapter 3])
 * Challenge 1: Python Fundamentals & Modules (Complete Practice 1 [Chapter 3])

Question
Write a function named  which will randomly generate a lower case letter and return the random character generated.

Solution
Use chr to help you here (you will find out about chr in your notes, alternatively run  in python)

You will also need to import random.

Question
Write a function named  to compute and return the cube of an input number. Note that you are not allowed to use the Math module for this question.

Solution
Use number**power to raise a number to a certain power (ie. x**y == xy).

Question
Consider the diagram below:



Given:



Write a function  to calculate the refractive index of medium i and return the value. Note that you should round your answer to 3 decimal places.

ai = the angle of incidence in degrees

ar = the angle of refraction in degrees

nr = the refraction index of medium containing the transmitted ray

Remarks: the angle in degree need to convert to the angle in radians first before calculating its sine value.

Solution
The math module has 2 things you need here: math.radians and math.sin.

Convert angles to radians, then run it through Snell's Law.

Question
The formula for computing the area of a pentagon is



, where s is the length of a side.

The side can be computed using the formula



where r is the length from the centre of a pentagon to a vertex.

For example



Write a program which will perform the following tasks:

Sample Output:
 * ask the user to enter the length from the center of a pentagon to a vertex
 * computes the area of the pentagon using the formulas given above and round the answer to 3 decimal places.
 * store the value of area in a variable named
 * prints the area
 * prints the area

Enter r: 5.5

Area = 108.611

Solution
Use the math module for sin. This time, you do not need to convert π/5 to radians, as it is already in radians.

Do not forget to round the area to 3 decimal places.

Also do note you are not writing a function in this question.