简体   繁体   English

python代码正在打印内存地址而不是函数中的数据

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

I'm completely at a loss as to the issue I have tried multiple fixes found on the internet that I thought could fix the code but it is still printing the memory location instead of the data.对于这个问题,我完全不知所措,我尝试了在互联网上找到的多个修复程序,我认为这些修复程序可以修复代码,但它仍在打印内存位置而不是数据。 I'm completely new to python and trying to teach myself for a coding class that is coming up.我对 python 完全陌生,并试图自学即将到来的编码课程。 The program should prompt the user for the number of rooms and the level of cleaning then print the total cost per room and total overall cost.程序应提示用户房间数量和清洁程度,然后打印每个房间的总成本和总成本。

''' room_qty = float(input("Please enter the total number of rooms :")) cleantype = str(input('Please choose cleaning intensity (light or heavy) :')) ''' 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()

''' '''

Any help in resolving this issue would be greatly appreciated.解决此问题的任何帮助将不胜感激。

You are not calling the function.您没有调用该函数。 When you write-当你写 -

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

it just prints the function instead of calling it and getting a value.它只是打印函数而不是调用它并获取一个值。 Make sure to call the function.确保调用该函数。 For your case, it's the following-对于您的情况,如下所示-

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

In your code you have #Output phase where you are printing some text and a function.在您的代码中,您有#Output phase ,您可以在其中打印一些文本和一个函数。 If you don't use parenthesis after the funtion name you will print the memory address of the object of the function.如果函数名称后不使用括号,则会打印函数对象的内存地址。 To actually call the function you will need to use parenthesis.要实际调用该函数,您需要使用括号。

Here is what you do now:这是你现在要做的:

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

Here is what you should do:这是你应该做的:

 # 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)))

Remember that you will need to add room_qty as an argument to your function total_clean_cost and not just empty braces as you do for clean_cost() .请记住,您需要将room_qty作为参数添加到函数total_clean_cost ,而不仅仅是为clean_cost()所做的空括号。


Edit:编辑:

Also, is there a reason that you have your function definitions inside the main function?另外,是否有理由在主函数中包含函数定义? Otherwise this might not be the best approach.否则这可能不是最好的方法。

The use cases of Python inner functions are varied. Python 内部函数的用例多种多样。 You can use them to provide encapsulation and hide your functions from external access, you can write helper inner functions, and you can also create closures and decorators.您可以使用它们来提供封装并隐藏您的函数以防止外部访问,您可以编写辅助内部函数,还可以创建闭包和装饰器。

Read more about it here: https://realpython.com/inner-functions-what-are-they-good-for/在此处阅读更多信息: https ://realpython.com/inner-functions-what-are-they-good-for/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 python 中的 PostgreSQL 打印 bytea 数据的问题,但总是得到<memory at 0x>地址</memory> - Issues with printing bytea data from PostgreSQL in python, but always get <memory at 0x> address 如何从内存地址重建Python函数? - How to reconstruct Python function from memory address? 是否可以返回字典的值(对象列表)并调用它们的 __str__ function 而不是打印它们的 memory 地址? - Is it possible to return a dictionary's values (list of objects) and call their __str__ function instead of printing their memory address? Python-chain.from_iterable返回内存地址而不是值 - Python-chain.from_iterable returns memory address instead of value "我的链表代码是在 python 中打印地址" - my code for linked list is printing address in python 在 Python 中打印函数定义和调用的地址 - Printing the address of the function definition and calls in Python 使用python将对象追加到列表中会抛出对象内存地址,而不是对象内部的数据 - Appending an object to a list with python is throwing object memory address instead of the data inside the object python中的mmap打印二进制数据而不是文本 - mmap in python printing binary data instead of text 在Python中调用函数句柄时的内存地址 - Memory address when function handle called in Python 使用 Python 从变量打印 PDF,而不是文件 - Printing a PDF with Python from variable, instead a file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM