Python解释器说paintRequiredCeiling是未定义的。我无法在代码中找到任何错误。目标是让程序从用户处获得输入,然后计算油漆作业所需的成本/小时数。.ceil()数学函数不工作?
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()
这些变量是'calculate'函数的局部变量,因此您分配的值在'printFunction'中不可见。 – Barmar
它是'calculate'函数的局部变量。您需要将其返回,然后作为参数传播到'printFunction'。 – BartoszKP
如果您收到错误消息,则必须准确告诉我们错误是什么以及发生了什么。我们不应该执行你的代码或研究它来发现你的错误。 – Gabe