计算宝石商店的账单

时间:2018-12-04 22:41:07

标签: python logic

下面是我的代码,用于计算gem商店的账单。 我能够从列表中为正确的元素获取正确的价格,但无法弄清楚如何将它们与所需数量(reqd_qty列表)相乘并计算总价格。 < / p>

以下是完整的问题描述。

Tanishq宝石商店向其客户出售各种宝石。

编写一个Python程序,根据宝石清单和购买数量计算客户要支付的账单金额。如果商店中没有客户需要的宝石,请考虑将总账单金额设为-1。

假设客户要求的任何宝石数量始终大于0

def calculate_bill_amount(gems_list, price_list, reqd_gems,reqd_quantity):
bill_amount=0
#Write your logic here
for gem in gems_list:
    for g in reqd_gems:
        if g==gem:
            index = gems_list.index(g)
            price = price_list[index]
            print(price) 


return bill_amount

#List of gems available in the store
gems_list=["Emerald","Ivory","Jasper","Ruby","Garnet"]

#Price of gems available in the store. gems_list and price_list have one-to- 
one correspondence
price_list=[1760,2119,1599,3920,3999]

#List of gems required by the customer
reqd_gems=["Ivory","Emerald","Garnet"]

#Quantity of gems required by the customer. reqd_gems and reqd_quantity have 
one-to-one correspondence
reqd_quantity=[3,2,5]

bill_amount=calculate_bill_amount(gems_list, price_list, reqd_gems, 
reqd_quantity)
print(bill_amount)

当前输出:

1760 # Ivory Price
2119 # Emerald Price
3999 # Garnet Price
0 # Bill amount

5 个答案:

答案 0 :(得分:1)

我建议您阅读字典,循环遍历列表以匹配另一个列表中的值是非常低效的。 https://docs.python.org/3/tutorial/datastructures.html#dictionaries

if(k in stuff)
    stuff[k].nullify;    //I suppose that this part could actually be deleted.
else{
    Nullable!(V)a;       //Create a Nullable object
    a.nullify;           //nullify it but this part may not be needed
    stuff[k] = a;        //Assign to stuff[k] which sets the isNull data field to true
}

答案 1 :(得分:0)

正如您在早些时候删除同一问题之前的答复中所指出的:bill_amount应该只是您计算过程中所有价格的总和,因此在执行print(price)命令之后,尝试添加bill_amount += price。这会将bill_amount的值设置为其当前值加上price的新值。尽管您仍未将此价格乘以数量。

此外,您的代码无法解决在可用列表中没有gem的情况。因此,一种解决方法是跳过计算,如果g不在gems_list中,则返回-1。您可以使用命令“ if g not in gems_list:”。

def calculate_bill_amount2(gems_list, price_list, reqd_gems,reqd_quantity):
    bill_amount=0
    #Write your logic here
    for gem in gems_list:
        for g in reqd_gems:
            if g==gem:
                index = gems_list.index(g)
                no_of_gems = reqd_quantity[reqd_gems.index(g)]
                price = price_list[index] * no_of_gems
                print(price)
                bill_amount += price
            if g not in gems_list:
                return -1                
    return bill_amount

坦白说,我只使用字典而不是列表,因为您正在做很多不必要的循环:

def calculate_bill_amount(gem_prices, reqd_gem_quantities):
    bill_amount = 0
    for gem in reqd_gem_quantities: 
        if gem in gem_prices:
            price = gem_prices[gem] * reqd_gem_quantities[gem]
            bill_amount += price
        else:
            return -1
    return bill_amount

gem_prices = { "Emerald": 1760, "Ivory": 2119, "Jasper": 1599, "Ruby": 3920, "Garnet": 3999 }
reqd_gem_quantities = { "Ivory": 3, "Emerald": 2, "Garnet": 5 }

print(calculate_bill_amount(gem_prices, reqd_gem_quantities))

答案 2 :(得分:0)

程序逻辑有两个主要缺陷。 第一个原因是您永远不会更新 bill_amount ,这就是为什么您的答案返回0的原因。 其次,价格没有乘以所需数量。 这是重做的calculate_bill_amount():

def calculate_bill_amount(gems_list, price_list, reqd_gems,reqd_quantity):
bill_amount=0
#Write your logic here
x=0 
for g in reqd_gems:
    for gem in gems_list:
        if g==gem:
            index = gems_list.index(g)
            price = price_list[index]
            bill_amount += price * reqd_quantity[x] 
            print(bill_amount, price, reqd_quantity[x], x)
    x+=1
return(bill_amount)

@Bill .M也指出您没有考虑宝石不在列表中的情况是正确的

答案 3 :(得分:0)

def calculate_bill_amount(gems_list, price_list, reqd_gems,reqd_quantity):
    bill_amount=0
    #Write your logic here
    j=0
    for i in reqd_gems:
        if i in gems_list:
            index=gems_list.index(i)
            bill_amount=bill_amount+reqd_quantity[j]*price_list[index]
            j=j+1
        else:
            bill_amount=-1
            break
    if(bill_amount>30000):
        bill_amount=bill_amount-(bill_amount*5/100)

    return bill_amount

答案 4 :(得分:0)

def calculate_bill_amount(gems_list, price_list, reqd_gems,reqd_quantity):
bill_amount=0
dict1={}
dict2={}
k=0
for i in reqd_gems:
    dict2[i]=reqd_quantity[k]
    k+=1
k=0
for i in gems_list:
    dict1[i]=price_list[k]
    k+=1
for i in dict2.keys():
    gem=i
    if gem in dict1.keys():
        bill_amount+=dict1[gem]*dict2[gem]
    else:
        bill_amount=-1
        break
if bill_amount>30000:
    bill_amount=bill_amount-(bill_amount*5/100)
return bill_amount

*上面的代码工作正常..在这里我创建了两个字典并映射了对应关系..但我也是编程新手,所以我不知道它是否有效*