CSCI1101:Assignment 2

 Conditionals

Assigned: Thursday, September 6
Due: Tuesday, September 18 at 11:59 PM

Again, two programs:  The first asks you to do a different version of the triangle program.

The second automatically makes a college admissions decision based on various criteria (test scores, GPA, class rank).

'Good design principles' are very much a part of this assignment.  It is not enough to solve the problems:  the form of your solution should also conform to the specifications given in each problem.  Above all, this means having the main computational work performed by functions that neither read from the keyboard nor write to the screen, and using the same order of arguments, function names, and return types as in the specification.

Sections 5.2-5.7 of the Think  Python book are relevant.

1.  Area of a Triangle, Revisited

30 points. 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 here 2+3<6.

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


istriangle(a,b,c)

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.  If istriangle returns False, 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:


The admitted function

30 points. Your submission must contain a function

admitted(sat,act,rank,gpa)

that returns True or False, 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

admitted(0,30,0.15,3.1)

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.

Main program


30 points. 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:
0
Enter the ACT score.  Type 0 if you did not take the ACT:
29
Does your high school rank its graduates?  Answer 'yes' of 'no'.
yes
What is your rank in class?
41
What is the size of your high school graduating class?
180
Enter your weighted GPA from high school.
3.6
Unfortunately, you do not qualify for admission.



Enhancement: Validate the data.


10 points. 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

Write another function

isvalid(sat,act,rank,gpa)

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

import admission

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 function responds.

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

import admission 

You can run the main program just by launching this one-line program, and after it has run, test the admitted function as above.