0

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()
4
  • If I run this code, it doesn't tell me the function isn't defined. It does tell me NameError: name 'price' is not defined. price quite obviously isn't defined anywhere, and is probably not meant to be a function. (This, by the way, is exactly why you should post the actual error message, not just describe it.) Commented Sep 25, 2014 at 23:29
  • Actually, if you fix two other errors, then you will get an error about the function retun not being defined, because that typo happens to make your return statement look like an expression with a call to a function named retun in it… Commented Sep 25, 2014 at 23:32
  • @Marius, meant retun Commented Sep 25, 2014 at 23:34
  • Also, not related to your code, but you don't specify to the user that the dimensions they're entering are in square feet. :) Commented Sep 25, 2014 at 23:42

3 Answers 3

1

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()
Sign up to request clarification or add additional context in comments.

6 Comments

True, but he isn't even getting that far.
Wow I did not see that, but it still says: NameError: name 'price' is not defined
@Redblaster13: Yeah, as Padraic points out, there are at least 3 major errors in your code; it's hard to fault anyone for not being able to guess which one you're going to see first without running it… With a slight change, this one would be a SyntaxError and prevent you from getting to the NameError
Im brand new to functions sorry, can you please tell me how to fix it?
Yeah, so you know how you are telling paint_cost to take in the price? It should really take in the dimen, right? because you're calculating the paint_cost based on the dimen. The price is fixed to $40.
|
1

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

1 Comment

Ah thank you so much didn't realize I should put dimen as the parameter.
-1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.