初学者:使用 void 函数计算 Python 中的地毯成本?
Beginner: Using a void function to calculate carpet cost in Python?
以下是此作业的说明:
编写一个名为 carpet.py 的程序,该程序使用一个名为 carpet_cost 的空函数来计算和显示铺设矩形房间的成本。该函数应将以英尺为单位的房间长度、以英尺为单位的房间宽度以及每平方码的地毯成本作为参数。这三个值应该从主函数中的用户输入中获取。 carpet_cost 函数应该计算并以货币格式显示地毯的成本,第一个数字前面有一个 $ 符号,如果金额以千为单位,则用逗号分隔,小数点后两位。
这是我目前的情况,
length = int(input("What is the length of tbe room you're trying to carpet?: "))
width = int(input("What is the width of tbe room you're trying to carpet?: "))
cost = int(input("What is the cost per square yard of tbe room you're trying to carpet?: "))
def carpet_cost(total_cost):
total_cost == ((length * width) / 3) * cost
print('The cost to carpet this room will be $', format(total_cost, ',.2f'))
carpet_cost(total_cost)
我在如何将它变成 void 函数时遇到了问题,我试图到处寻找,但我还没有找到实现它的方法。我确实知道如何通过仅使用一个函数来使用更简单的方法来计算它,但我真的很困惑 void 函数如何帮助提供相同的输出。
如果我的代码不好,我们将不胜感激和抱歉,但这是因为我很难全神贯注于 void 函数。
参见 Python void-like function。 Python 是动态类型的,它的功能总是 return 的东西。如果您不指定 return 值,python 会自动 returns None
.
此外,您将 total_cost
作为 carpet_cost
的参数,这是计算 total_cost
的函数。我认为你需要检查一下你的逻辑。
如果 "void" 你的意思是 "doesn't return anything",你已经有一个 void 函数。
您的 carpet_cost
函数采用一个参数 total_cost
,但它应该采用长度、宽度和每平方码成本三个参数。此外,您应该使用赋值运算符 =
而不是相等性测试运算符 ==
.
length = int(input("What is the length of tbe room you're trying to carpet?: "))
width = int(input("What is the width of tbe room you're trying to carpet?: "))
cost = int(input("What is the cost per square yard of tbe room you're trying to carpet?: "))
def carpet_cost(l,w,c):
total_cost = ((l * w) / 3) * c
print('The cost to carpet this room will be $', format(total_cost, ',.2f'))
carpet_cost(length,width,cost)
结果:
What is the length of tbe room you're trying to carpet?: 3
What is the width of tbe room you're trying to carpet?: 3
What is the cost per square yard of tbe room you're trying to carpet?: 50
The cost to carpet this room will be $ 150.00
另外,您在计算总成本时似乎有一道算术题。如果要将平方英尺转换为平方码,则必须除以九,而不是三。
其中一个可能就是您想要的
#example 1
def carpet_cost(length,width,cost_per_unit):
return length*width*cost_per_unit
print carpet_cost(length=5,width=10,cost_per_unit=2.10)
#example 1.0005
def carpet_cost(length,width,cost_per_unit):
print length*width*cost_per_unit
#example 2 (pass by reference, since a list is editable)
def carpet_cost(args_list):
length,width,cost_per_unit = args_list
args_list[:] = [length*width*cost_per_unit]
length,width,cost = 5,10,1.12
result = [length,width,cost] # a list is editable
carpet_cost(result)
print result
以下是此作业的说明:
编写一个名为 carpet.py 的程序,该程序使用一个名为 carpet_cost 的空函数来计算和显示铺设矩形房间的成本。该函数应将以英尺为单位的房间长度、以英尺为单位的房间宽度以及每平方码的地毯成本作为参数。这三个值应该从主函数中的用户输入中获取。 carpet_cost 函数应该计算并以货币格式显示地毯的成本,第一个数字前面有一个 $ 符号,如果金额以千为单位,则用逗号分隔,小数点后两位。
这是我目前的情况,
length = int(input("What is the length of tbe room you're trying to carpet?: "))
width = int(input("What is the width of tbe room you're trying to carpet?: "))
cost = int(input("What is the cost per square yard of tbe room you're trying to carpet?: "))
def carpet_cost(total_cost):
total_cost == ((length * width) / 3) * cost
print('The cost to carpet this room will be $', format(total_cost, ',.2f'))
carpet_cost(total_cost)
我在如何将它变成 void 函数时遇到了问题,我试图到处寻找,但我还没有找到实现它的方法。我确实知道如何通过仅使用一个函数来使用更简单的方法来计算它,但我真的很困惑 void 函数如何帮助提供相同的输出。
如果我的代码不好,我们将不胜感激和抱歉,但这是因为我很难全神贯注于 void 函数。
参见 Python void-like function。 Python 是动态类型的,它的功能总是 return 的东西。如果您不指定 return 值,python 会自动 returns None
.
此外,您将 total_cost
作为 carpet_cost
的参数,这是计算 total_cost
的函数。我认为你需要检查一下你的逻辑。
如果 "void" 你的意思是 "doesn't return anything",你已经有一个 void 函数。
您的 carpet_cost
函数采用一个参数 total_cost
,但它应该采用长度、宽度和每平方码成本三个参数。此外,您应该使用赋值运算符 =
而不是相等性测试运算符 ==
.
length = int(input("What is the length of tbe room you're trying to carpet?: "))
width = int(input("What is the width of tbe room you're trying to carpet?: "))
cost = int(input("What is the cost per square yard of tbe room you're trying to carpet?: "))
def carpet_cost(l,w,c):
total_cost = ((l * w) / 3) * c
print('The cost to carpet this room will be $', format(total_cost, ',.2f'))
carpet_cost(length,width,cost)
结果:
What is the length of tbe room you're trying to carpet?: 3
What is the width of tbe room you're trying to carpet?: 3
What is the cost per square yard of tbe room you're trying to carpet?: 50
The cost to carpet this room will be $ 150.00
另外,您在计算总成本时似乎有一道算术题。如果要将平方英尺转换为平方码,则必须除以九,而不是三。
其中一个可能就是您想要的
#example 1
def carpet_cost(length,width,cost_per_unit):
return length*width*cost_per_unit
print carpet_cost(length=5,width=10,cost_per_unit=2.10)
#example 1.0005
def carpet_cost(length,width,cost_per_unit):
print length*width*cost_per_unit
#example 2 (pass by reference, since a list is editable)
def carpet_cost(args_list):
length,width,cost_per_unit = args_list
args_list[:] = [length*width*cost_per_unit]
length,width,cost = 5,10,1.12
result = [length,width,cost] # a list is editable
carpet_cost(result)
print result