简体   繁体   中英

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. 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) :'))

    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. 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() .


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. 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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM