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)

main()

———————————————————————————————————-

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

———————————————————————————————————

Advertisements

0 Responses to “First Program of the Semester”



  1. Leave a Comment

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




“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,357 hits

%d bloggers like this: