1. Area of a Triangle, Revisited
Let's go back to the simpler version of the
triangle program, where you simply entered the lengths of the
three sides of the triangle and found the area. If your
input numbers are, say, 2, 3, and 6, then the program blows
up. The explosion occurs because the area
function tried to take the square root of a negative number. The
geometric explanation is that you can't make a triangle whose
sides are 2,3 and 6: In a triangle, the lengths of any two
sides must add to at least the length of the third side, but
In your program, you should apply a 'look before you leap'
strategy: The program should check to see whether the
three entered lengths form a valid triangle before
calling the area
function. So, you will leave the area
function unchanged, but add a new function
that will take the three side lengths and return True
or False, depending on whether the three side
lengths really form a triangle.
The main program will call istriangle
before the call to area
, then your main program will
print an informative error message, and end gracefully.
Otherwise it will proceed to call area
and print the result, as before.
2. College Admissions
The admissions criteria below were taken, with some slight
changes, from published standards for admission of new Freshmen
to the Honors College at a large midwestern university. To be
eligible for admission the student must:
- Have a combined Math and Verbal SAT at least 1250, OR a
composite ACT score of at least 28.
- If the student's high school ranks its students, be in the
top 20% of the graduating class, and have a weighted GPA of
at least 3.0
- If the student's high school does not rank its students,
have a weighted GPA of at least 3.5.
The admitted function
Your submission must contain a function
that returns True
, depending on whether the
criteria are met. The parameter sat
is an integer representing the combined SAT score, or 0, if the
SAT was not taken. The parameter act
likewise represents the ACT score, or 0 if the ACT was not
taken. The parameter rank
is a float between 0 and 1: If the high school ranks its
students, then this parameter has the value
(rank in class)/(size of graduating class).
For example, if a student graduates 40th in a class of 200, this
parameter has the value 40/200=0.2 (i.e., the student is in the
top 20%, but just barely!). If the high school does not
rank its students, then this parameter has the value 0.
The parameter gpa
of course is
a float between 0.0 and 4.0.
You should write and test this function first, before tackling
the main program. If you run the resulting module, you can then
type calls to the function in the Python shell to see if you are
getting the correct values. For example, if you type
then you should see the result True
because this student has a 30 on the ACT, did not take the
SAT, is in the top 15% of the graduating class, and has a
GPA of 3.1. On the other hand, if you type
admitted(1300, 0, 0, 3.2)
then you should see the result False, because the student's GPA
is 3.2 but their school does not rank its graduates.
The main program should carry on a dialogue
with the user, asking for the necessary information. Once that
information is obtained, the result of the admission decision is
printed. Here is a typical example (user responses are
indicated in red.)
Enter the SAT score. Type 0 if you did not
take the SAT:
Enter the ACT score. Type 0 if you did not take
Does your high school rank its graduates?
Answer 'yes' of 'no'.
What is your rank in class?
What is the size of your high school graduating
Enter your weighted GPA from high school.
Unfortunately, you do not qualify for admission.
Enhancement: Validate the data.
If the user enters bad data, the program
should not crash, but it should not give an admissions decision
either. Instead it should report that there was an error
and terminate. This will happen if
- The SAT score is not between 0 and 1600 inclusive.
- The ACT score is not between 0 and 36 inclusive.
- The rank is less than 0 or greater than 1. (Recall
that 0 indicates that the high school does not rank its
- The GPA is less than 0.0 or greater than 4.0.
Write another function
that returns True if the
data is valid, and False
otherwise. This should be called from the main program
before the call to admitted.
To see a working version of the program...
I have provided a .pyc file containing a working version of
the complete program. This will allow you to run the program
without seeing the code itself. Download the file into
the folder in which you do the work for this course. If
you're using IDLE, run any program that you already have in
the folder (this will tell IDLE to treat the folder as its
current directory). Then type
at the prompt. The program should begin executing. You
can also type things like
admission.admitted(1300, 0, 0, 3.2)
at the prompt, to see how the individual
Note added later: These instructions for running the admitted function
work properly, but you will only be able to run the main
program once using them; if you want to run it
again, you will have to restart the process: reset the
shell, run any program in your folder, and type import admission. Here is another way to proceed that avoid
this problem: Download the .pyc file as above. Use
IDLE to create a new file, called whatever.py (because you can call it whatever you want, as
long as you have the .py extension) and that contains just
the single line
You can run the main program
just by launching this one-line program, and after it has
run, test the admitted function as above.