簡體   English   中英

python代碼正在打印內存地址而不是函數中的數據

[英]python code is printing memory address instead of data from the function

對於這個問題,我完全不知所措,我嘗試了在互聯網上找到的多個修復程序,我認為這些修復程序可以修復代碼,但它仍在打印內存位置而不是數據。 我對 python 完全陌生,並試圖自學即將到來的編碼課程。 程序應提示用戶房間數量和清潔程度,然后打印每個房間的總成本和總成本。

''' room_qty = float(input("請輸入房間總數:")) cleantype = str(input('請選擇清潔強度(輕或重):'))

    def cleantype_choiceprog(cleantype):  
        cleanchoice = 10
        if cleantype == "light" or cleantype == "l":
            cleanchoice = 10
        elif cleantype == "heavy" or cleantype == "h":
            cleanchoice = 20
        return cleanchoice

    def cleansize_selectionprog(room_qty): 
        roomcost = 0
        if room_qty >= 1 or room_qty <= 7:
            roomcost = 1.0  
            return roomcost
        elif room_qty >= 8 or room_qty <= 15:
            roomcost = .8  
            return roomcost
        elif room_qty >= 16 or room_qty <= 25:
            roomcost = .6  
            return roomcost
        elif room_qty >= 25:
            roomcost = .4  
            return roomcost
        else:
            print('Please enter a value greater than 0')
            cleansize_selectionprog(room_qty)
        return roomcost
 
    def clean_cost():
        cc = (cleantype_choiceprog(cleantype) * cleansize_selectionprog(room_qty))
        return cc

    def total_clean_cost(room_qty):
        tcc = clean_cost() * room_qty
        return tcc

    print('It will cost :', clean_cost, ':per room cleaned.')
    print('The total cost of cleaning this space is: ', str(total_clean_cost))
    


main()

'''

解決此問題的任何幫助將不勝感激。

您沒有調用該函數。 當你寫 -

print('It will cost :', clean_cost, ':per room cleaned.')

它只是打印函數而不是調用它並獲取一個值。 確保調用該函數。 對於您的情況,如下所示-

print('It will cost :', clean_cost(), ':per room cleaned.')

在您的代碼中,您有#Output phase ,您可以在其中打印一些文本和一個函數。 如果函數名稱后不使用括號,則會打印函數對象的內存地址。 要實際調用該函數,您需要使用括號。

這是你現在要做的:

# Output Phase
    print('It will cost :', clean_cost, ':per room cleaned.')
    print('The total cost of cleaning this space is: ', str(total_clean_cost))

這是你應該做的:

 # Output Phase
    print('It will cost :', clean_cost(), ':per room cleaned.')
    print('The total cost of cleaning this space is: ', str(total_clean_cost(room_qty)))

請記住,您需要將room_qty作為參數添加到函數total_clean_cost ,而不僅僅是為clean_cost()所做的空括號。


編輯:

另外,是否有理由在主函數中包含函數定義? 否則這可能不是最好的方法。

Python 內部函數的用例多種多樣。 您可以使用它們來提供封裝並隱藏您的函數以防止外部訪問,您可以編寫輔助內部函數,還可以創建閉包和裝飾器。

在此處閱讀更多信息: https ://realpython.com/inner-functions-what-are-they-good-for/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM