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()
变量
paintRequiredCeiling
仅在计算函数中可用。它不存在于你printFunction
。与其他变量类似。您需要将它们移到函数之外,或者传递它们,才能使其工作。在在您的
calculate()
函数中没有return
语句:您正在计算所有这些值,然后在函数结束时丢弃它们,因为这些变量都是函数的局部变量。在类似地,
printFunction()
函数不接受任何要打印的值。所以它期望变量是全局的,因为它们不是全局的,你就得到了错误。在现在您可以使用全局变量,但这通常是错误的解决方案。相反,学习如何使用
return
语句返回calculate()
函数的结果,将这些结果存储在main()
中的变量中,然后将它们传递给printFunction()
。在相关问题 更多 >
编程相关推荐