【发布时间】:2016-11-15 08:35:37
【问题描述】:
我需要编写一个函数,该函数接受一个字典作为库存以及一个 (name, number) 对的 product_list,它指示我们何时应该通过添加某个数字来更新该产品的库存,这可能是负数数字。
一旦第一次提到产品,它就会被添加到字典中,当它的计数达到零时,它会保留在字典中。如果计数变为负数,我需要引发值错误。
例子:
d = {"apple":50, "pear":30, "orange":25}
ps = [("apple",20),("pear",-10),("grape",18)]
shelve(d,ps)
d
{'pear': 20, 'grape': 18, 'orange': 25, 'apple': 70}
shelve(d,[("apple",-1000)])
Traceback (most recent call last):
ValueError: negative amount for apple
我的代码给出了意外的 EOF 错误或无效的语法,具体取决于我是否包含最后一个打印行。它目前绝对没有实现目标,但我相信这是我需要解决这个问题的格式和逻辑。我需要打印“x 的负数”的函数,其中 x 是负数。对此的任何帮助表示赞赏
代码:
def shelve(inventory,product_list):
count = 0
try:
for x in product_list:
if x == True:
product_list.append(x)
count += key
else:
return product_list
except ValueError:
print ('negative amount for (product)')
print "hello program starts here"
d = {"apple":50, "pear":30, "orange":25}
ps = [("apple",20),("pear",-10),("grape",18)]
shelve(d,ps)
【问题讨论】:
-
python 3 要求你使用
print("string") -
感谢刚刚解决了这个问题
-
鉴于大量的逻辑错误,您可能需要比从 Stack Overflow 获得的更多帮助。这看起来也像是一个家庭作业问题:如果是这种情况,我会向你们学校寻求额外的帮助。
-
您在检索参数后将参数设置为空字典和空列表。不要那样做。
-
欢迎来到 StackOverflow。请阅读并遵循帮助文档中的发布指南。 Minimal, complete, verifiable example 适用于此。在您发布代码并准确描述问题之前,我们无法有效地帮助您。
标签: python python-3.x dictionary math product