CMPT 120: Lab 2


Today's lab will have you working with if statements.

Remember to get marked off as complete when you finish. If you don't finish, make sure you are marked as present so you recieve that portion of the lab grade.

1. Write a Python program that prompts the user for student's grade, given as a percentage. It will then output the letter grade the student earned.

  • A: >= 85%
  • B: >= 75%
  • C: >= 65%
  • D: >= 55%
  • F: < 55%

The program should also output a message if the user inputs an invalid number, less than 0 or greater than 100. Here are a couple example runs.

Enter the student's percentage grade: 87.3
The student's letter grade is A.


Enter the student's percentage grade: 121
That is not a correct percentage. Enter a number between 0 and 100.


Use an if = elif = else structure. Start out by testing the first clause (checking for an A maybe) and adding clauses once you have the first one working.

2. Write a Python program that will compute the area of a triangle, or the area of a circle depending on the user's choice. Use 3.14 * r2 for the area of a circle. The area of a trianle is .5 * base * height. Print an error if the user enters a negative number. Here a couple example runs.

1. Find the area of a triangle.
2. Find the area of a circle.
------------------------------------
Select an option: 1
Enter the length of the base: 1.5
Enter the height of the triangle: 2
The area of the triangle is 1.5.


1. Find the area of a triangle.
2. Find the area of a circle.
------------------------------------
Select an option: 2
Enter the radius of the circle: 1
The area of the circle is 3.14.


1. Find the area of a triangle.
2. Find the area of a circle.
------------------------------------
Select an option: 4
That is not a valid option.


When you are finished you should have if statements nested inside the code blocks for a different if statement. Think of a way to code this one piece at a time so you can test each piece before you start the next.


Chris Schmidt, last updated May 23, 2007