Redblaster13
Redblaster13

Reputation: 11

It keeps saying my function is not defined in python

I am taking a intro to computer programming class and in it we use python.

My assignment is to Write a program named paint.py that will determine the cost of painting the walls of a shed with a rectangular floor. Assume the shed has no windows and that paint costs $40 per gallon. One gallon covers 300 square feet. Prompt the user to enter the dimensions of the shed. Use a function named paint_cost that takes the user inputs as arguments and returns the cost of painting the walls of the shed. Express the cost in currency format.

I have tried really hard on thinking how to do it. I have researched and read the chapter over and over again in my python book. So if anyone can please help me.

def paint_cost(price):
    return (dimen / 300) * 40
def main():
    dimen = input('Enter the dimensions of the shed: ')
    print('Your cost of painting will be $', paint_cost(price))
main()

Upvotes: 0

Views: 9919

Answers (3)

Padraic Cunningham
Padraic Cunningham

Reputation: 180411

I think this is closer to what you are trying to do:

def paint_cost(dimen):
    return (dimen / 300.) * 40  # calculates cost based on dimension
def main():
    dimen = int(input('Enter the dimensions of the shed: ')) ]  # cast input as an integer
    print('Your cost of painting will be ${}'.format(paint_cost(dimen)))# pass the dimensions to paint_cost and print using `str.format`
main()

errors in your original code:

retun should be return so a syntax error

(dimen / 300) * 40 dimen only exists in the main function so an undefined error

paint_cost(price) price is not defined, so another undefined error

Upvotes: 1

Jess
Jess

Reputation: 3146

Errors in your original code:

  • Line 2: retun should be return
  • Remove price from the paint_cost function, because you are calculating it on line 2.
  • Replace it with dimen

.

def paint_cost(dimen):
    cost = (dimen / 300) * 40
    return cost
def main():
    dimen = int(input('Enter the dimensions of the shed: '))
    print 'Your cost of painting will be $ %s' % str(paint_cost(dimen))
main()

Upvotes: 1

tom10
tom10

Reputation: 69192

In your line:

print('Your cost of painting will be $', paint_cost(price))

price has not been defined.

Usually Python will give a good description of what's wrong, as it did for me here when I ran this:

NameError: global name 'price' is not defined

There are other problems, but work your way through, paying particular attention to the tracebacks.

Upvotes: -1

Related Questions