Python 油漆作业估计器四舍五入
我想创建一个程序来计算油漆工作的成本。每125平方英尺的墙壁空间需要一加仑油漆和八小时的劳动。该公司的人工费为每小时42.50美元。程序应要求用户输入要涂漆的墙面面积平方英尺以及每加仑的价格。该程序将显示以下内容:Python 油漆作业估计器四舍五入,python,rounding,Python,Rounding,我想创建一个程序来计算油漆工作的成本。每125平方英尺的墙壁空间需要一加仑油漆和八小时的劳动。该公司的人工费为每小时42.50美元。程序应要求用户输入要涂漆的墙面面积平方英尺以及每加仑的价格。该程序将显示以下内容: 所需油漆的加仑数四舍五入为整加仑 所需的劳动时间 油漆的成本是按整加仑加起来计算的 劳务费 油漆工作的总成本 不允许输入负数、零和非数字;加仑数应始终四舍五入;应在末尾出现提示,要求进行另一次估算;工时应显示为小数点后一位;加仑油漆应显示为整数值,小数点右侧不显示任何内容;总人工
- 所需油漆的加仑数四舍五入为整加仑
- 所需的劳动时间
- 油漆的成本是按整加仑加起来计算的
- 劳务费
- 油漆工作的总成本
print("This program calculates the cost of doing a paint job.")
do_calculation = True
while (do_calculation):
while (True):
try:
# Input amount of square feet of wall space
square_feet = float(input("How many square feet of wall space do you need painted? "))
if (square_feet <= 0):
print ("Zero and negative numbers can not be accepted.")
continue
except ValueError:
print("The value you entered is invalid. Only numericals are valid.");
else:
break
while(True):
try:
# Input cost per gallon
cost_per_gallon = float(input("Cost per gallon of paint? $"))
if (cost_per_gallon <= 0):
print ('Zero and negative numbers can not be acepted.')
continue
except ValueError:
print("The value you entered is invalid. Only numericals are valid.");
else:
break
while(True):
try:
# Input number of gallons needed
gallons_needed = float(square_feet)/125.0
except ValueError:
print("The value you entered is invalid. Only numericals are valid.");
else:
break
# Hours of labor expected
hours_of_labor = 8 * gallons_needed
# Cost of Labor expected
labor_costs = 42.5 * hours_of_labor
# Paint cost
paint_costs = float(cost_per_gallon) * gallons_needed
# Number of gallons of paint
print("The number of gallons of paint required is %f" % gallons_needed)
# How long labor lasts
print("The number of gallons of hours of labor required are %f" % hours_of_labor)
# Charged for labor
print("The labor charges are $%f" % labor_costs)
# Total amount of labor
print("Total labor is: %f" % (labor_costs + paint_costs))
another_calculation = input("\nDo you want to try another estimate? (y/n): ")
if (another_calculation != "y"):
do_calculation = False
print(“此程序计算喷漆作业的成本。”)
do_计算=真
而(进行计算):
虽然(正确):
尝试:
#输入墙空间的平方英尺数量
平方英尺=浮动(输入(“您需要绘制多少平方英尺的墙空间?”)
如果(平方英尺)
事实上,我有三个四舍五入的整数:你是怎么得到的
工时显示到小数点后一位(例如:26.5小时),如何操作
将油漆的加仑数显示为不带小数的整数值(例如:
6) ,以及如何将人工费总额显示为小数点后两位
积分(例如:144.69美元)
你应该在你的实际问题中加上这些
通过向输出的字符串中添加各种参数来控制数字的格式设置。请记住,$144.69
是一个字符串,不是有效的数字
您已经在代码中使用了其中的一些(%f
是一个说明符),您只需修改这些
最后-取整是关于总小时数,你必须注意的事情,在这种情况下,你必须取整(就像现实生活中的大多数承包商一样)看看内置的帮助
所需加仑=圆形(浮子((平方英尺)/125.0),1)
?这会使
用户的输入总是四舍五入用于计算
您可以试试看:
>>> round(1.24, 1)
1.2
>>> round(1.25, 1)
1.3
因此,这里的答案是否定的,它不会总是四舍五入的。您可以阅读文档以获得关于如何使它总是四舍五入的更多提示
一个小麻烦:打印(“人工费是:
$”,(人工成本))“该程序在
金额和美元:'劳务费是:$XXX.XX'我该如何处理这些费用
之间的空间
它打印空格是因为您将两个参数传递给print函数,它只是一个接一个地打印:
>>> print('The costs are $',(i))
The costs are $ 144.43
如果您传递一个字符串,它将正确打印。这里我传递一个字符串,参数格式为字符串(%s
):
注意这里的()
是不必要的(它们不做任何有用的事情)。您可以忽略它们:
>>> print('The cost are $%s' % i)
The cost are $144.43
您还可以使用较新的字符串格式迷你语言,如下所示:
>>> print('The costs are ${}'.format(i))
The costs are $144.43
你的问题是什么?我实际上有三个四舍五入:如何将工时显示为一个小数点(例如:26.5小时),如何将加仑油漆显示为不带小数点的整数值(例如:6),以及如何将总工时显示为两个小数点(例如:144.69美元),比如:加仑油漆=四舍五入(浮动)((平方英尺)/125.0),1)?这会让用户的输入总是四舍五入进行计算吗?还有一个小麻烦:使用“打印”(“人工费是:$”,(人工费)),程序会在金额和$之间添加一个恼人的空格:“人工费是:$XXX.XX”,我如何去掉两者之间的空格?
>>> print('The costs are ${}'.format(i))
The costs are $144.43