Python in school

So you may have noticed that I have been busy lately. I took a break from the ruby to prepare for the college placement exam. I know it’s just high school stuff, but I have been out of high school for more than fifteen years. The good news is that I tested well enough to begin college level courses.

Through a convoluted chain of events I ended up taking a course that I had not planned on, but looked fun anyway; Programming Logic and Design. It turns out that they use python to teach the basics of program design (and logic). I could probably test out of this course, but it looks like fun and maybe I will even become a better programmer by taking it.

The first lab assignment was this:

“Write a program that will take in basic information from a student, including student name, degree name, number of credits taken so far, and the total number of credits required in the degree program. The program will then calculate how many credits are needed to graduate. Display should include the student name, the degree name, and credits left to graduate.”

So hopped right into the code and quickly had this:

#!/usr/bin/python
# program to get and display student information w/ input checking

# text entry functions
def get_name():
    studentName = raw_input('Enter student name. ')
    while not check_alpha(studentName):
        studentName = raw_input('Enter student name. ')
    return studentName

def get_degree():
    studentDegree = raw_input('Enter student\'s degree. ')
    while not check_alpha(studentDegree) :
        studentDegree = raw_input('Enter student\'s degree. ')
    return studentDegree

def get_credits_degree():
    creditsDegree = raw_input('Enter the number of credits required for degree. ')
    while not check_numeric(creditsDegree) :
        creditsDegree = raw_input('Enter the number of credits required for degree. ')
    return creditsDegree

def get_credits_taken():
    creditsTaken = raw_input('Enter the number of credits student has taken. ')
    while not check_numeric(creditsTaken) :
        creditsTaken = raw_input('Enter the number of credits student has taken. ')
    return creditsTaken

# begin input checking functions

# check for numeric input
def check_alpha(alpha):
    if alpha.strip() == "" :
        print 'This field cannot be left blank. '
        return False
    elif alpha.isdigit() :
        print 'This field is not for numeric input. '
        return False
    else :
        return  True

# check for non-numeric input
def check_numeric(numeric):
    if numeric.strip() == "" :
        print 'This field cannot be left blank. '
        return False;
    elif not numeric.isdigit() :
        print 'This field is numeric only. '
        return False
    else :
        return True

# check credits left
def get_credits_left():
    creditsLeft = int(creditsDegree) - int(creditsTaken)
    if creditsLeft == 0 :
        creditsLeft = "0 Congradulations!"
    elif creditsLeft < 0 :
        creditsLeft = str(creditsLeft) + "\nMaybe you should stop changing your major."
    return creditsLeft

# assign values
studentName = get_name()
studentDegree = get_degree()
creditsDegree = get_credits_degree()
creditsTaken = get_credits_taken()
creditsLeft = get_credits_left()

# output values
print "+---------------------------------------------------------+"
print "The student's name is ", studentName
print "The degree program is ", studentDegree
print "Credits left to graduation is ", creditsLeft
print "+---------------------------------------------------------+"

Then I remembered that the instructor had made a pretty big deal about how we have to write whatever code the customer wants and that in our case the customer is the instructor. Looking through the lab documentation they pretty much walk you right through writing most of the program (making you find syntax errors in samples etc.) and nowhere do they do any input validation. I start to think that maybe I should just write the program they specify that craps out if you feed it garbage input. So I went back to the drawing board and wrote this:

#!/usr/bin/python
# program to get and display student information

# text entry sections
studentName = raw_input('Enter student name. ');
studentDegree = raw_input('Enter student\'s degree. ');
creditsDegree = input('Enter the number of credits required for degree. ');
creditsTaken = input('Enter the number of credits student has taken. ');
creditsLeft = creditsDegree - creditsTaken;

#output section
print "The student's name is ", studentName
print "The degree program is ", studentDegree
print "Credits left to graduation is ", creditsLeft

As awful as it is that is the code that matches the pseudo code and the flow charts in the lab manual. Afterwards I felt so bad that I did the rest of the labs with full validation of inputs. I can’t in good conscience write error prone code, unless of course it means my grade, or you know, some one wants to pay me to.

Oh, and anybody out there taking this class, no cheating off my code now.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s