# tpm.py Computes the cost per square inch of pizza given cost of the pizza,
# and diameter.


from math import *

def main():
cost_per_square_inch(area_of_pizza())

def area_of_pizza():
diameter = input("Enter the diameter of the pizza: ")
radius = 1.0/2.0 * diameter
a = pi * radius * radius
return a

def cost_per_square_inch(area):
cost_of_pizza = input("Enter the cost of the pizza: ")
cost = cost_of_pizza/area
print "The cost per square inch of pizza is $%0.2f"%(cost)
return
main()

:?: :o

Recommended Answers

All 2 Replies

Please wrap you code in code tags, see:
http://www.daniweb.com/techtalkforums/announcement114-3.html

This way the proper indentations are added.

I would not pass function on as argument, so rewrite you code:

from math import *  # to get pi

def main():
    # first get the area of the whole pizza
    area = area_of_pizza()    
    cost_per_square_inch(area)

def area_of_pizza():
    diameter = input("Enter the diameter of the pizza: ")
    radius = diameter/2.0
    a = pi * radius * radius
    return a

def cost_per_square_inch(area):
    # get the cost of the whole pizza
    cost_of_pizza = input("Enter the cost of the pizza: ")
    # calculate the cost per area unit
    cost = cost_of_pizza/area
    print "The cost per square inch of pizza is $%0.2f" % (cost)


main()

Since you only need math.pi you don't have to import all math functions/constants. You could use:

from math import pi

Remember one of the basic rules of computing, ideally the flow in a program should be:

get data

process data

show result

After I said this, I got interested myself to write the flow, here is my overblown code:

# find cost of a square inch of pizza given cost and size/diameter
# a small program to show flow of data via functions

def main():
    # get data
    cost = get_cost()
    size = get_size()
    # process data
    area = calc_area(size)
    cost_sqinch = cost_per_area(cost, area)
    # present result
    show_result(cost_sqinch)

def get_cost():
    # avoid using input(), raw_input() gets a string, so convert to float
    return float(raw_input("Enter the cost of the pizza: "))

def get_size():
    # again, avoid using input()
    return float(raw_input("Enter the diameter of the pizza: "))

def calc_area(diameter):
    """calculate the area of a circle"""
    pi = 3.1416  # good enough
    radius = diameter/2.0
    return pi * radius * radius

def cost_per_area(cost, area):
    return cost/area

def show_result(cost_sqinch):
    print "The cost per square inch of pizza is $%0.2f" % (cost_sqinch)
    
main()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.