0

Python interpreter say that paintRequiredCeiling is undefined. I was unable to find any errors in the code. The objective is for the program to take input from the user, then calculate the costs/ hours needed for a paint job.

import math

def main():
    # Prompts user for sq and paint price
    totalArea = float(input("Total sq of space to be painted? "))
    paintPrice = float(input("Please enter the price per gallon of paint. "))

    perHour = 20
    hoursPer115 = 8

    calculate(totalArea, paintPrice, perHour, hoursPer115)
    printFunction()

def calculate(totalArea, paintPrice, perHour, hoursPer115):
    paintRequired = totalArea / 115
    paintRequiredCeiling = math.ceil(paintRequired)
    hoursRequired = paintRequired * 8
    costOfPaint = paintPrice * paintRequiredCeiling
    laborCharges = hoursRequired * perHour
    totalCost = laborCharges + costOfPaint

def printFunction():
    print("The numbers of gallons of paint required:", paintRequiredCeiling)
    print("The hours of labor required:", format(hoursRequired, '.1f'))
    print("The cost of the paint: $", format(costOfPaint, '.2f'), sep='')
    print("Total labor charges: $", format(laborCharges, '.2f'), sep='')
    print("Total cost of job: $", format(totalCost, '.2f'), sep='')

main()
  • 4
    The variables are local to the `calculate` function, so the values you assigned are not visible in `printFunction`. – Barmar Oct 02 '13 at 19:56
  • It's local variable of `calculate` function. You need to return it, and then propagate to `printFunction` as a parameter. – BartoszKP Oct 02 '13 at 19:56
  • If you are getting an error message, you have to tell us exactly what the error is and where it happens. We shouldn't have to execute your code or study it to find your error. – Gabe Oct 02 '13 at 19:56
  • See http://stackoverflow.com/questions/370357/python-variable-scope-question – Barmar Oct 02 '13 at 19:56
  • This has nothing at all to do with `math.ceil()`. – kindall Oct 02 '13 at 19:59

2 Answers2

1

The variable paintRequiredCeiling is only available in your calculate function. It doesn't exist in you printFunction. Similarly with other variables. You'll need to move them outside of the functions, or pass them, to get this to work.

AlG
  • 14,697
  • 4
  • 41
  • 54
1

There's no return statement in your calculate() function: you're calculating all these values, then throwing them away when your function ends, because those variables are all local to the function.

Similarly, your printFunction() function doesn't accept any values to print. So it expects the variables to be global, and since they're not, you get the error you got.

Now you could use global variables, but that is typically the wrong solution. Instead, learn how to use the return statement to return the results of your calculate() function, store those in variables in main(), and then pass them to printFunction().

kindall
  • 178,883
  • 35
  • 278
  • 309