Posts Tagged 'Python'

Big, Big News!

After 14 months of hard work, aggravation beyond description, despair of the blackest shade, and, yes, tears, a watershed event. I, erstwhile DI major, terrible math scholar, and programmer par non-excellence, have accomplished something that I have long dreamed about, talked about, and now, finally, accomplished.

Send in the band, please

I have accessed a database, extracted some data, and reported the results, using Python (nifty programming language). Just now, I literally dance for joy. The wife is stunned.

Herewith the details…..

The Code:

# A program to access the MaryRichards database, retrieve
# the tuples for the JOBS table, display as output the JOB id, job description
# and amount billed for the job.

import pyodbc

cnxn=pyodbc.connect(“DSN=MaryRichardsBackup;UID=The Tortoise;PWD=lucky7”)

except pyodbc.Error, error:
print “Error — No access”
print “Connected”
cursor = cnxn.cursor()

# Select some values from the database and print them:

cursor.execute(“select * from job”)
allrecs = cursor.fetchall()
for row in allrecs:
print row.JOB_ID, row.Description, row.AmountBilled


The Result:

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type “copyright”, “credits” or “license()” for more information.

Personal firewall software may warn about the connection IDLE
makes to its subprocess using this computer’s internal loopback
interface. This connection is not visible on any external
interface and no data is sent to or received from the Internet.

IDLE 1.2.1
>>> ================================ RESTART ================================
1 Paint exterior in 794 White 2750.0000
2 Paint dining room and kitchen 1778.0000
3 Prep and paint upstairs bath 550.0000
4 Paint exterior doors in 633 Red 885.0000
5 Prep and paint interior wood trim 1299.0000

I Exult………


First Program of the Semester

So the professor asks us to write a program in Python, after which she will show us the same program written in Java. The program was easy to develop conceptually, and the programming was, for the most part, easy. There were a few looks back into our Python Quick Reference, but not bad after a 4 week layoff. We’ll see what she thinks about the algorithm and the design. I expect the Java version to be way more complex, from a syntax point of view.

Here’s my code…..feel free to comment; it’s already been submitted so your thoughts will not incur an Honor violation.


import math

def ordQty(): # Get order quantity

n = input(“How many bags of coffee do you want to order? “)

while n <= 0: # Makes sure order is larger than 0

print”Invalid quantity. Your order must be at least 1 bag.”

n = input(“how many bags of coffee do you want to order? “)

return n

def discountTable(n, cost): # creates discount based on volume (per assignment parameters)

if n < 25:
discount = cost * 0.0

if n >= 25 and n < 50:
discount = cost * .05

if n >= 50 and n< 100:
discount = cost * .10

if n >= 100 and n< 200:
discount = cost * .15

if n >= 200 and n < 250:
discount = cost * .20

if n >= 250 and n < 300:
discount = cost * .25

if n >= 300:
discount = cost * .30

return discount

def boxTable(n): # calculates number of boxes required for order

A = n / 20 # A is the variable for large boxes. Returns an integer value

remA = n % 20 # remA calculates the remainder value for the A box divisor

B = remA / 10 # B is the variable for the medium boxes. Returns an integer value

remB = remA % 10 # remB calculates the remainder value for the B box divisor

C = remB / 5.0 # C is the variable for the small boxes. Returns a float value

Last = math.ceil(C) # Use ‘ceil’ function to determine next highest integer

costA = A * 2.0 # costA is the box charge for large boxes

costB = B * 1.0 # costB is the box charge for medium boxes

costLast = Last * .50 # costLast is the box charge for small boxes

boxCharge = (costA + costB + costLast) # Calculate total box cost for return

return boxCharge

def main():

n = ordQty()

cost = n * 5.50 # calculate cost of coffee before any discount

print “You have ordered”,n,”bags of coffee. The price is $%0.2f” % (cost)

discount = discountTable(n, cost) # create discount function, return discount amount

print “Your quantity discount is $%0.2f” % (discount)

boxCharge = boxTable(n) # calculate number of boxes required and cost

print”The box charge is $%0.2f” % (boxCharge)

totalCost = cost – discount + boxCharge # calculate order total

print “The total cost of your order is $%0.2f” % (totalCost)



The output of a test run:


How many bags of coffee do you want to order? 999
You have ordered 999 bags of coffee. The price is $5494.50
Your quantity discount is $1648.35
The box charge is $100.00
The total cost of your order is $3946.15


We Love Python


“Life’s hard, son. It’s harder when you’re stupid.” — The Duke.

Education is a companion which no misfortune can depress, no crime can destroy, no enemy can alienate,no despotism can enslave. At home, a friend, abroad, an introduction, in solitude a solace and in society an ornament.It chastens vice, it guides virtue, it gives at once grace and government to genius. Without it, what is man? A splendid slave, a reasoning savage. - Joseph Addison
The term informavore (also spelled informivore) characterizes an organism that consumes information. It is meant to be a description of human behavior in modern information society, in comparison to omnivore, as a description of humans consuming food. George A. Miller [1] coined the term in 1983 as an analogy to how organisms survive by consuming negative entropy (as suggested by Erwin Schrödinger [2]). Miller states, "Just as the body survives by ingesting negative entropy, so the mind survives by ingesting information. In a very general sense, all higher organisms are informavores." - Wikipedia

Blog Stats

  • 30,792 hits