2013-10-02 22 views
0

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

这些变量是'calculate'函数的局部变量,因此您分配的值在'printFunction'中不可见。 – Barmar

+0

它是'calculate'函数的局部变量。您需要将其返回,然后作为参数传播到'printFunction'。 – BartoszKP

+0

如果您收到错误消息,则必须准确告诉我们错误是什么以及发生了什么。我们不应该执行你的代码或研究它来发现你的错误。 – Gabe

回答

1

变量paintRequiredCeiling只适用于您的计算功能。它不存在于你printFunction。与其他变量类似。你需要将它们移到函数之外,或者传递它们,以使其发挥作用。

1

您的calculate()函数中没有return语句:您正在计算所有这些值,然后在函数结束时将它们扔掉,因为这些变量都是函数的局部变量。

同样,您的printFunction()函数不接受任何要打印的值。所以它期望变量是全局的,因为它们不是,你会得到你的错误。

现在你可能使用全局变量,但这通常是错误的解决方案。相反,请学习如何使用return语句返回calculate()函数的结果,将这些变量存储在main()中,然后将它们传递给printFunction()