【发布时间】:2021-09-29 21:56:56
【问题描述】:
我起草定制橱柜,有时需要将运行分成 3 个橱柜,我不能让每个橱柜截面相等,因为截面宽度必须是英制的(到 1/32 英寸)并且不能有三分之一英寸。我通常需要做一些粗略的数学运算并根据需要调整宽度以保持运行的总长度并保持英制测量,但我正在编写一个 python 程序来为我计算这个。我承认我是Python 的 hack,但我正在尽力而为。 这就是我到目前为止所拥有的 - 1)如何将每个截面宽度四舍五入到最接近的 1/32 英寸,同时 2)保持整体宽度?(即,不四舍五入到最接近的值,因为它可能会影响整体宽度) 谢谢!
import math
# input total length of run
run_length = float(input("Enter length of the run, in inches:"))
# accounting for 1/8" reveals
reveal_num = float(input("Enter number of reveals: "))
reveal_length = reveal_num * 0.125
true_length = run_length - reveal_length
# determining number of run sections
section_num = float(input("Enter desired number of doors/sections: "))
# This is where the fun begins
# First group is nicely divisible into whole numbers
if true_length % section_num == 0:
section_length = true_length / section_num
print("Each section will be", section_length, "inches")
# Second group is divisible by 32nds, tested by multiplying by 32 and seeing if result is a
whole number
# Then to be rounded to the nearest 1/32" while maintaining the total width
elif ((true_length / section_num) * 32).is_integer():
# Third group is the misfits. Must be divided to the nearest 1/32" while maintaining the total
width.
else:
【问题讨论】:
-
你可能会从How to make rounded percentages add up to 100%得到一些启发。
标签: python rounding integer-division