[英]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.