程序停止处理大量数据

发布于 2025-01-17 01:37:30 字数 923 浏览 0 评论 0原文

我创建了一个精彩的代码,考虑到背包的最大允许重量,通过重复枚举返回列表中的物品,总结它们的价格和重量,并给出结果。目前,我面临的问题是,当最大权重变大(例如 80)时,代码根本无法运行。我真的不明白这是否是无限循环或优化的问题,所以我将感谢您的帮助!

backpack_max = int(input('Backpack limit: '))
item_list = ['Rune', 'Arrows', 'Rock', 'Sword']
item_weight = [2, 4, 5, 10]
item_price = [20, 4, 1, 15]
backpack_fin_items = []
total_weight = 0
total_price = 0
count = 0

while total_weight+min(item_weight) < backpack_max:
  for item, price in zip(item_list, item_price):
    if total_weight+item_weight[count] <= backpack_max:
      total_weight += item_weight[count]
      backpack_fin_items.append(item.lower())
      total_price += price
      count += 1
      count %= len(item_list)
      joint = ', '.join(backpack_fin_items)
  
print (f'You are packed with loot! \n\nContent: {joint}\n\nTotal weight: {total_weight}/{backpack_max} kg\nTotal price: {total_price} coins\n\nHave a nice adventure, warrior!')

I created a wonderful code that, taking into account the maximum allowable weight of a backpack, returns the items from the list by repeated enumeration, sums up their price and weight, and gives the result. At the moment, I am facing the problem that the code simply does not run when the maximum weight becomes large (for example 80). I don't really understand if this is a problem with infinite loops or optimization, so I would appreciate your help with it!

backpack_max = int(input('Backpack limit: '))
item_list = ['Rune', 'Arrows', 'Rock', 'Sword']
item_weight = [2, 4, 5, 10]
item_price = [20, 4, 1, 15]
backpack_fin_items = []
total_weight = 0
total_price = 0
count = 0

while total_weight+min(item_weight) < backpack_max:
  for item, price in zip(item_list, item_price):
    if total_weight+item_weight[count] <= backpack_max:
      total_weight += item_weight[count]
      backpack_fin_items.append(item.lower())
      total_price += price
      count += 1
      count %= len(item_list)
      joint = ', '.join(backpack_fin_items)
  
print (f'You are packed with loot! \n\nContent: {joint}\n\nTotal weight: {total_weight}/{backpack_max} kg\nTotal price: {total_price} coins\n\nHave a nice adventure, warrior!')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微凉 2025-01-24 01:37:30

并不是你的物品太多,而是你陷入了无法向背包添加任何物品的状态。要调试这样的小程序,一个有用的方法是在代码中执行您不期望的操作的部分中粘贴 print

while total_weight+min(item_weight) < backpack_max:
    print(total_weight, backpack_fin_items)
    # ...

在无限循环中打印此内容:

74 ['rune', 'arrows', 'rock', 'sword', 'rune', 'arrows', 'rock', 'sword', 'rune', 'arrows', 'rock', 'sword', 'rune', 'arrows', 'rock']
74 ['rune', 'arrows', 'rock', 'sword', 'rune', 'arrows', 'rock', 'sword', 'rune', 'arrows', 'rock', 'sword', 'rune', 'arrows', 'rock']

这并不直观,因为看起来我们应该能够添加重量为 2 的项目。

更仔细地查看代码:

    for item, price in zip(item_list, item_price):
        if total_weight+item_weight[count] <= backpack_max:

我怀疑问题出在 item_weight[count] - 我不清楚如何获得与 item 相关的权重,或者 count 应该做什么。

如果我只是将权重添加到您的 zip 表达式中,并删除引用 count 的所有行,否则效果很好:

    for item, price, weight in zip(item_list, item_price, item_weight):
        if total_weight + weight <= backpack_max:
            total_weight += weight

It's not that you have too many items, it's that you get stuck in a state where you're unable to add any items to the backpack. To debug small programs like this, a helpful thing is to stick a print in the part of the code that's doing something you don't expect.

while total_weight+min(item_weight) < backpack_max:
    print(total_weight, backpack_fin_items)
    # ...

prints this in an infinite loop:

74 ['rune', 'arrows', 'rock', 'sword', 'rune', 'arrows', 'rock', 'sword', 'rune', 'arrows', 'rock', 'sword', 'rune', 'arrows', 'rock']
74 ['rune', 'arrows', 'rock', 'sword', 'rune', 'arrows', 'rock', 'sword', 'rune', 'arrows', 'rock', 'sword', 'rune', 'arrows', 'rock']

which is not intuitive, since it seems like we should be able to add items of weight 2.

Looking more closely at the code:

    for item, price in zip(item_list, item_price):
        if total_weight+item_weight[count] <= backpack_max:

I suspect the problem is with item_weight[count] -- it's not clear to me how that would get you the weight associated with item, or what count is supposed to be doing.

If I just add the weights to your zip expression and delete all the lines that reference count otherwise, it works fine:

    for item, price, weight in zip(item_list, item_price, item_weight):
        if total_weight + weight <= backpack_max:
            total_weight += weight
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文