我希望项目键和项目值使用if语句与query1和query2匹配

时间:2018-06-07 21:17:39

标签: python

def product():
    item = {
        'Bananas': 3,
        'Apples' : 2,
        'Oranges' : 4,
        'Pineapple' : 6,
        'Strawberry' : 5
    }

query1 = input('Write product: ')
query2 = int(input('Write quantity: '))

print(product())

1 个答案:

答案 0 :(得分:1)

我假设您正在寻找True / False作为函数的结果。

 def product():
    item = { 'Bananas': 3, 'Apples' : 2, 'Oranges' : 4, 'Pineapple' : 6, 'Strawberry' : 5 }
    query1 = input('Write product: ')
    query2 = int(input('Write quantity: '))
    if query1 in item:
        if item[query1] == query2:
            return True
    return False

print(product())