Python解释器说paintRequiredCeiling是未定义的。我无法在代码中找到任何错误。目标是让程序从用户那里获取输入,然后计算油漆作业所需的成本/小时数。
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()
答案 0 :(得分:1)
变量paintRequiredCeiling
仅在您的计算函数中可用。你printFunction
中不存在它。与其他变量类似。您需要将它们移出函数之外,或者传递它们才能使它工作。
答案 1 :(得分:1)
return
函数中没有calculate()
语句:您正在计算所有这些值,然后在函数结束时抛弃它们,因为这些变量都是函数的本地变量。
同样,您的printFunction()
函数不接受任何要打印的值。因此,它希望变量是全局变量,因为它们不是,所以你会得到错误。
现在你可以使用全局变量,但这通常是错误的解决方案。相反,请了解如何使用return
语句返回calculate()
函数的结果,将其存储在main()
中的变量中,然后将其传递给printFunction()
。