Comp 150 – Computer Programming 1

February 27, 2008



1.         isLeapYear(year) – a boolean valued function; also demonstrate exception handling using try:  except:

 

#

#  Name:

#  File: LeapYear02.py

#  Date: February 24, 2008

#

#  Desc: Demonstrates a boolean values function

#

 

def isLeapYear(year):

    #

    #  returns True if year is a leap year

    #

    return (year % 4 == 0) and (year % 100 != 0 ) or (year % 400 == 0)

 

def main():

 

    #  display title

 

    print "\nLeap Year Detector\n"

 

    #  ask-before-iterating loop

 

    again = "y"

    while (again == "y"):

 

        try:                                           # check for exceptions

            year = input("Enter a year: ")

            if isLeapYear(year):

                print "%d is a leap year" % year

            else:

                print "%d is not a leap year" % year

 

        except TypeError:

            print "\nType Error: Entry should be a number\n"

        except NameError:

            print "\nName Error: Identifier entered, need a number\n"

        except Syntax Error:

            print “\nSyntax Error: Illegal syntax used for input\n”  

        except:

            print "\nUnknown error\n"

 

        again = raw_input("\nAgain? (y/n): ")

 

main()

 

2.         Gross Pay Examples: First program calculates regular, overtime and gross pay given hours worked and pay rate. Second is same as first except logical blocks of code are consolidated into functions and function calls are used in main program.

 

#

#  Name:

#  File:  GrossPay.py

#  Date:  February 27, 2008

#

#  Desc:  Computes gross pay given hour worked and pay rate

 

def main():

 

    #  display title

 

    print "\nGross Pay Program\n"

 

    #  get hours worked and pay rate

 

    hoursWorked = input("Enter hours worked: ")

    payRate = input("Enter hourly pay rate: ")

 

    #   compute regular, overtime, and gross pay

 

    if (hoursWorked <= 40.0):

        regularPay = hoursWorked * payRate

        overtimePay = 0.0

    else:

        regularPay = 40.0 * payRate

        overtimePay = (hoursWorked - 40.0) * payRate * 1.5

    grossPay = regularPay + overtimePay

 

    #  display results

 

    print

    print "Regular Pay  = $%6.2f" % regularPay

    print "Overtime Pay = $%6.2f" % overtimePay

    print "Gross Pay    = $%6.2f" % grossPay

    print  

 

main()

 

Second program is like first except it makes use of function calls

 

#

#  Name: 

#  File:  GrossPay02.py

#  Date:  February 27, 2008

#

#  Desc:  Same as gross pay program except uses function calls

#

 

def getHoursAndPayRate():

    #

    #  gets and returns hours worked and pay rate

    #

    h = input("Enter hours worked: ")

    p = input("Enter hourly pay rate: ")

    return h, p

 

def calculatePay(h, p):

    #

    #  calculates and returns regular, overtime and gross pay

    #  given hours worked and pay rate

    #  

    if (h <= 40.0):

        regularPay = h * p

        overtimePay = 0.0

    else:

        regularPay = 40.0 * p

        overtimePay = (h - 40.0) * p * 1.5

    grossPay = regularPay + overtimePay

    return regularPay, overtimePay, grossPay

 

def displayPay(regularPay, overtimePay, grossPay):

    #

    #  displays regular, overtime and gross pay

    #

    print

    print "Regular Pay  = $%6.2f" % regularPay

    print "Overtime Pay = $%6.2f" % overtimePay

    print "Gross Pay    = $%6.2f" % grossPay

    print  

 

def main():

 

    # display title

 

    print "\nGross Pay Program (witn functiobn calls)\n"

 

    # get hours worked and pay rate

 

    hoursWorked, payRate = getHoursAndPayRate()

 

    #  compute regular, overtime and gross pay

  

    regularPay, overtimePay, grossPay = calculatePay(hoursWorked, payRate)

 

    #  display results

 

    displayPay(regularPay, overtimePay, grossPay)

 

main()