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