Newbie learning OOP 2nd ?

LenS lsumnler at uniqueinsuranceco.com
Mon May 30 08:35:31 EDT 2005


I have coded this little program which is a small little tip calculator
program.  I am in the process of trying to convert this program to use
OOP.  Would appreciate others more experienced in OOP code in how they
might do it.

Would be happy to forward all profits from the sale of the program;-))

#Program name:  tipcalc05.py
#This program calculates the dollar amount of a tip on a resturant bill
import datetime
import string

def calc_full_bill():
    '''This function is used to get information regarding the bill and
       calculate the tax, tip, and total amount of the bill'''
    global billamt
    global tippct
    global taxpct
    global totalbill

    billamt = raw_input("Please enter the bill amount: ")
    tippct = raw_input("Please enter the percent tip: ")
    location = getanswers("Are you in Chicago, ")

    if location == True:
        taxpct = 7.25
    else:
        taxpct = 6.75

    tipamt = calcpct(billamt, tippct)

    taxamt = calcpct(billamt, taxpct)

    totalbill = float(billamt) + taxamt + tipamt

    print "Tip = %.2f Tax = %.2f Total = %.2f" % (tipamt, taxamt,
totalbill)

def group_bill_process():
    grpbill = open("grpbill.txt", 'a+')
    group = raw_input("Please give the name of the group? ")

    for line in grpbill:
        print string.split(line, sep=',')

    who_paid = raw_input("Who paid the bill? ")
    grpdate = datetime.datetime.now()
    group_detail = group + ',' + who_paid + ',' + str(totalbill) + ','
+ str(grpdate) + '\n'
    grpbill.write(group_detail)

def calc_bill_by_guest():
    '''This function allows you to get multiple amounts for any number
       of individuals and calculate each individuals share of the tip
       tax and bill amount'''

    guest = []
    netbill = float(billamt)
    while True:
        gf_name = raw_input("Enter girlfriends name: ")
        gf_amt = raw_input("Enter girlfriends amount: ")
        guest.append((gf_name, gf_amt))
        netbill = netbill - float(gf_amt)
        print "Amount remaining %.2f" % netbill
        anymore = getanswers("Anymore girlfriends, ")
        if anymore == False:
            break

#these print lines are here just to show the tuples within a list

    print guest
    print

    for (g, a) in (guest):
        gf_tax = calcpct(a, taxpct)
        gf_tip = calcpct(a, tippct)
        gf_total = float(a) + gf_tax + gf_tip
        print "%s Tip = %.2f Tax = %.2f Total = %.2f" % (g, gf_tip,
gf_tax, gf_total)
        print

def getanswers(question):
    '''This function allows you to print some question looking for
       and answer of the form Yes or No'''
    answer = raw_input(question + "'Yes' or 'No': ")
    print
    if answer in ["Yes", "yes", "Y", "y", "OK", "ok", "Ok", "yeh",
"Yeh"]:
        return(True)
    else:
        return(False)

def calcpct(bill, pct):
    '''This function calculates the amount base off of some
       given percentage'''
    amt = float(bill) * float(pct) / 100
    return(amt)

while True:
    calc_full_bill()

    group_bill = getanswers("Is this a group bill? ")
    if group_bill == True:
        group_bill_process()
    else:
        split_the_bill = getanswers("Do you want to equally spilt the
Bill, ")
        if split_the_bill == True:
            no_of_dinners = raw_input("Split between how many people?
")
            print "Each person owes %.2f" % (float(totalbill) /
float(no_of_dinners))
        else:
            calc_bill_by_guest()

    runagain = getanswers("Do you want to run again, ")

    if runagain == False:
        break


PS If there is somewhere else I should be posting this kind of thing
let me know otherwise thanks everyone for your help.

Len Sumnler




More information about the Python-list mailing list