我正试图编写一些小程序,帮助我计算墙上需要多少装饰元素,以及元素之间的距离
假设墙的宽度为128.3厘米,元素的宽度为4.3厘米
我需要一些公式,这将帮助我计算元素之间的差距(面板),这样,我就不必削减元素的宽度
也请参考附件中的图片
以下是我目前的代码:
wall_length = float(128.3)
panel_width = float(4.3)
print('\n','\n','\n','\n')
num1 = (float(wall_length) / float(panel_width))
num2 = num1 / 2
num3 = num2 + 1
num4 = num3 * float(panel_width)
num5 = wall_length - num4
num6 = num5 / num2
num7 = num6 + panel_width
print('###########################', '\n', 'num1 - result! Divide wall with Panel', '\n', '=', num1, '\n')
print('###########################', '\n', 'num2 - result!', '\n', num1, '/', '2', '=', num2, '\n')
print('###########################', '\n', 'num3 - result!', '\n', num2, '+', '1', '=', num3, '\n')
print('###########################', '\n', 'num4 - result!', '\n', num3, '*', panel_width, '=', num4, '\n')
print('###########################', '\n', 'num5 - result!', '\n', wall_length, '-', num4, '=', num5, '\n')
print('###########################', '\n', 'num6 - result!', '\n', num5, '/', num2, '=', num6, '\n')
print('\n', '\n', '\n','\n', '\n', '\n')
print('###########################','\n','YOUR GAP SIZE IS','\n',num6,'\n' )
print('###########################','\n','YOUR PANEL SIZE + GAP SIZE','\n',num7,'\n')
print('###########################')
print('\n', '\n', '\n','\n', '\n', '\n')
或者是另一种解决方案,获得与面板宽度几乎相同的间隙宽度
输出:
您使用哪个版本的python?该程序至少需要3.6,因为
f-string
首先,我需要说几句话:
wall_length = float(128.3)
wall_length = 128.3
也会这样做李>num1
在代码中),因为墙应该以面板开始和结束李>128.3 / 43 = 29.837
您可以尝试29、31,看看哪一个是最好的李>以下是一种方法:
对于
128.3
和4.3
,调用print_possible_gap_widths(128.3, 4.3)
,结果将是:因此,最好的方法是
4.557142857142858
对于
128.3
和8.9
,调用print_possible_gap_widths(128.3, 8.9)
,结果将是:因此,最好的方法是
8.157142857142858
最后,您可以比较结果并作出决定
相关问题 更多 >
编程相关推荐