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()

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,097
  • 4
  • 41
  • 53
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
  • 168,929
  • 32
  • 262
  • 294