程序停止处理大量数据
我创建了一个精彩的代码,考虑到背包的最大允许重量,通过重复枚举返回列表中的物品,总结它们的价格和重量,并给出结果。目前,我面临的问题是,当最大权重变大(例如 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
并不是你的物品太多,而是你陷入了无法向背包添加任何物品的状态。要调试这样的小程序,一个有用的方法是在代码中执行您不期望的操作的部分中粘贴
print
。在无限循环中打印此内容:
这并不直观,因为看起来我们应该能够添加重量为 2 的项目。
更仔细地查看代码:
我怀疑问题出在
item_weight[count]
- 我不清楚如何获得与item
相关的权重,或者count
应该做什么。如果我只是将权重添加到您的
zip
表达式中,并删除引用count
的所有行,否则效果很好: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.prints this in an infinite loop:
which is not intuitive, since it seems like we should be able to add items of weight 2.
Looking more closely at the code:
I suspect the problem is with
item_weight[count]
-- it's not clear to me how that would get you the weight associated withitem
, or whatcount
is supposed to be doing.If I just add the weights to your
zip
expression and delete all the lines that referencecount
otherwise, it works fine: