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()
3 Answers 3
Errors in your original code:
- Line 2:
retunshould bereturn - Remove
pricefrom thepaint_costfunction, 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()
6 Comments
SyntaxError and prevent you from getting to the NameError...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ドル.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
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.
NameError: name 'price' is not defined.pricequite 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.)retunnot being defined, because that typo happens to make your return statement look like an expression with a call to a function namedretunin it...retun