【问题标题】:python code is printing memory address instead of data from the functionpython代码正在打印内存地址而不是函数中的数据
【发布时间】:2022-07-06 21:12:40
【问题描述】:

对于这个问题我完全不知所措,我尝试了在互联网上找到的多个修复程序,我认为这些修复程序可以修复代码,但它仍在打印内存位置而不是数据。我对 python 完全陌生,并试图自学即将到来的编码课程。程序应提示用户房间数量和清洁程度,然后打印每个房间的总成本和总成本。

'''

def main():
    # Welcome Message
    print('-'*20)
    print('Welcome to the Happy Sweeper project cost calculator.')
    print("-"*20)
    # Information Gathering Phase User Selections Gathered and Evaluated.
    room_qty = float(input("Please enter the total number of rooms :"))
    cleantype = str(input('Please choose cleaning intensity (light or heavy) :'))

    def cleantype_choiceprog(cleantype):  # Code to determine intensity.
        cleanchoice = 10
        if cleantype == "light" or cleantype == "l":
            print('You have chosen light cleaning. This will cover sweeping and dusting and basic sanitizing.')
            cleanchoice = 10
        elif cleantype == "heavy" or cleantype == "h":
            print('You have chosen heavy cleaning. This includes light cleaning and mopping, and heavy sanitizing.')
            cleanchoice = 20
        return cleanchoice

    def cleansize_selectionprog(room_qty):  # Code to gather room size choice from customer
        roomcost = 0
        if room_qty >= 1 or room_qty <= 7:
            roomcost = 1.0  # for homes with 1 - 7 rooms total.
            return roomcost
        elif room_qty >= 8 or room_qty <= 15:
            roomcost = .8  # for homes with 8 - 15 rooms total.
            return roomcost
        elif room_qty >= 16 or room_qty <= 25:
            roomcost = .6  # for homes with 16 - 25 rooms total
            return roomcost
        elif room_qty >= 25:
            roomcost = .4  # for homes with over 25 rooms or for friends and family.
            return roomcost
        else:
            print('Please enter a value greater than 0')
            cleansize_selectionprog(room_qty)
        return roomcost

    # Formula Execution Area

    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

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


main()

'''

我们将不胜感激任何解决此问题的帮助。

【问题讨论】:

  • clean_cost 是一个函数对象。 clean_cost() 是调用该函数的方式。

标签: python memory


【解决方案1】:

您没有调用该函数。当你写-

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

它只是打印函数而不是调用它并获取一个值。确保调用该函数。对于您的情况,如下-

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

【讨论】:

    【解决方案2】:

    你必须在这里调用函数:

    # 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() 那样只添加空括号。

    【讨论】:

      猜你喜欢
      • 2021-12-10
      • 2015-09-21
      • 2011-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-09
      • 1970-01-01
      相关资源
      最近更新 更多