Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3构建一个简单的健身工具_Python_Python 3.x - Fatal编程技术网

Python3构建一个简单的健身工具

Python3构建一个简单的健身工具,python,python-3.x,Python,Python 3.x,这只是一个有趣的项目,我认为做起来很酷,但我正在努力想办法 plates = [100, 45, 35, 25, 10, 5, 2.5] goal_weight = 425 starting_weight = 45 while goal_weight > starting_weight: 我的想法是用一个while循环遍历各个板块。我需要将每个数字最大化为目标重量(100乘以450 4次),然后移动到下一个数字并尝试,以显示加载杆的理想方式。但我可能走错了方向 示例:250=45磅杆(起

这只是一个有趣的项目,我认为做起来很酷,但我正在努力想办法

plates = [100, 45, 35, 25, 10, 5, 2.5]
goal_weight = 425
starting_weight = 45
while goal_weight > starting_weight:
我的想法是用一个while循环遍历各个板块。我需要将每个数字最大化为目标重量(100乘以450 4次),然后移动到下一个数字并尝试,以显示加载杆的理想方式。但我可能走错了方向

示例:250=45磅杆(起始重量),两个100磅板,两个2.5磅板 425=45磅杆,两个100磅,四个45磅


想要打印出两个100、两个45、两个10之类的内容,这里有一个小程序,可以找到正确的配重板组合。请注意,函数zip将配重板数量列表与配重列表相结合<代码>列表(zip(nweights,weights))形成元组列表,例如
[(4100),(0,45)…(0,2),(0,2.5)]


这就是我的结局。谢谢你们的帮助,伙计们

weights=[100, 45, 35, 25, 10, 5, 2.5]
target_weight = int(input('How much weight do you need? '))
bar_weight = int(input('Enter bar weight: '))

nweights = []
remaining = target_weight - bar_weight
for  i, weight in enumerate(weights):
    if int(remaining / weight) % 2 == 0:
        nweights.append(int(remaining/ weight))
    else:
        nweights.append(int(remaining/ weight) - 1)
    remaining = remaining - nweights[i]*weights[i]
    if remaining == 0:
        break

listweights=zip(nweights, weights)
print(f'{bar_weight}lb bar')
for weight in listweights:
    if weight[0] >= 2:
        print(f"{weight[0]} | {weight[1]}'s")

if remaining !=0:
    print(f'the correct weight combination cannot be found,'
          f'the remaining weight is: {remaining} pound')

是的,我认为你的解决方案是可行的,尽管下面的代码片段可能更符合逻辑。。。 (使用一些numpy数组方法)


查一下想要的输出是什么?顺便说一句,我不完全理解这里的问题输出应该是加载重量的最佳方式。这意味着要优先考虑较大的板块,而不是较小的板块。例如,如果你试图在杆的一侧加载100磅,最好加载一个100磅板,而不是两个45磅+一个10磅板,但我遇到的问题是,为了能够使用100磅,它必须能够使用两次,每侧一次。因此,如果你使用100磅的钢板,那么总重量将增加200磅,非常感谢!我打算玩玩它,试着更好地理解它。还是个傻瓜!洛拉,你知道我怎么调整它,如果它不能容纳两个盘子,那么它会移动到下一个重量吗?例如,对于230磅的输入,我得到的目标重量是什么:230你需要100磅的重量中的1磅你需要45磅的重量中的1磅你需要35磅的重量中的1磅你需要25磅的重量中的0磅你需要10磅的重量中的0磅你需要5磅的重量中的1磅我希望230磅的输出重量是:四个45磅,两个25磅。我的直觉是使用模%2==0,在这里的某个地方,只是不确定在哪里。所以你想要成对的重量(我想平衡杆…每端一个)我想你只需要把盘子翻一番,这样列表将是90,70,50,20等等。。。然后执行同样的算法。检查我上面发布的答案,这就是我在混乱了一段时间后得到的结果。就像我想象的那样!非常感谢你的帮助!
weights=[100, 45, 35, 25, 10, 5, 2.5]
target_weight = int(input('How much weight do you need? '))
bar_weight = int(input('Enter bar weight: '))

nweights = []
remaining = target_weight - bar_weight
for  i, weight in enumerate(weights):
    if int(remaining / weight) % 2 == 0:
        nweights.append(int(remaining/ weight))
    else:
        nweights.append(int(remaining/ weight) - 1)
    remaining = remaining - nweights[i]*weights[i]
    if remaining == 0:
        break

listweights=zip(nweights, weights)
print(f'{bar_weight}lb bar')
for weight in listweights:
    if weight[0] >= 2:
        print(f"{weight[0]} | {weight[1]}'s")

if remaining !=0:
    print(f'the correct weight combination cannot be found,'
          f'the remaining weight is: {remaining} pound')
import numpy as np

weights=np.array([100, 45, 35, 25, 10, 5, 2.5])
weights=weights*2
target_weight = int(input('How much weight do you need? '))

nweights=[]
remaining = target_weight
for i, weight in enumerate(weights):
    nweights=np.append(nweights, int(remaining/ weight))
    remaining = remaining - nweights[i]*weights[i]
    if remaining == 0:
        break

nweights = nweights*2
weights=weights*0.5
weightlist=zip(nweights, weights)

barweight=0
for weight in weightlist:
    print(f"{weight[0]} | {weight[1]}'s")
    barweight=barweight+weight[0]*weight[1]

print(f'total weight: {barweight} pound')

if remaining !=0:
    print(f'the correct weight combination cannot be found,'
          f'the remaining weight is: {remaining} pound')