2013-10-02 51 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()