在这个函数中有什么用,0

时间:2018-01-05 13:27:39

标签: python python-3.x

stock = {'football': 4, 'boardgame': 10, 'leggos': 100, 'doll': 5}    
def fillable(stock, merch, n):
      return stock.get(merch, 0) >= n

print(fillable(stock, 'leggos', 10))

代码战中的结果:

  

Traceback:in in fillable TypeError:unorderable types:   NoneType()> = int()

2 个答案:

答案 0 :(得分:0)

假设在这种情况下n是int(),尝试在比较之前将stock.get(merch,0)转换为int。

回答你的问题:https://stackoverflow.com/a/2068377/2774199

答案 1 :(得分:0)

以下是get()方法的语法 -

dict.get(key, default = None)
Parameters

key - 这是要在字典中搜索的密钥。

default - 这是在不存在密钥的情况下返回的值。

查看更多here

相关问题