Python3建立一个简单的健身

2024-05-15 19:48:38 发布

您现在位置:Python中文网/ 问答频道 /正文

这只是一个有趣的项目,我认为会很酷,但我正在努力找出它。你知道吗

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=45lb bar(起始重量),两个100lb板,两个2.5lb板 425=45磅杆,两个100磅,四个45磅

希望它能打印出:两个100,两个45,两个10


Tags: 项目示例目标方式数字方向酒吧starting
3条回答

这里有一个小程序,找到正确的组合重量板。注意函数zip,它将配重板的数量列表与配重列表相结合。list(zip(nweights, weights))形成元组列表,例如[(4, 100), (0, 45) ... (0, 2), (0, 2.5)]

weights=[100, 45, 35, 25, 10, 5, 2, 2.5]
targetweight = int(input('What is the target weight: '))

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

listweights=zip(nweights, weights)
for weight in listweights:
    print(f'you need {weight[0]} of weight {weight[1]} pound')

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

是的,我认为你的解决方案是可行的,不过也许下面的片段更符合逻辑。。。 (使用一些numpy数组方法)

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')

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

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')

相关问题 更多 >