问题在于,甚至计算器也应该写出答案,但是在网站上是错误的。
ARS宝石商店向其客户出售各种宝石。
编写一个Python程序,根据宝石清单和购买数量计算客户要支付的账单金额。账单总额超过30000卢比的任何购买都可享受5%的折扣。如果商店中没有客户需要的宝石,请考虑将总账单金额设为-1。
假设客户要求的任何宝石数量始终大于0。
在适用的情况下执行区分大小写的比较。
#List of gems available in the store
gems_list=['Amber', 'Aquamarine', 'Opal', 'Topaz']
#Price of gems available in the store. gems_list and price_list have one-to-one correspondence
price_list=[4392, 1342, 8734, 6421]
#List of gems required by the customer
reqd_gems=['Amber', 'Opal', 'Topaz']
#Quantity of gems required by the customer. reqd_gems and reqd_quantity have one-to-one correspondence
reqd_quantity=[2, 1, 3]
bill_amount=calculate_bill_amount(gems_list, price_list, reqd_gems, reqd_quantity)
print(bill_amount)
我希望输出为36781
这是我到目前为止提出的解决方案:
def calculate_bill_amount(gems_list, price_list, reqd_gems, reqd_quantity):
bill_amount = 0
for i in range(0, len(reqd_gems)):
if (reqd_gems[i] in gems_list) and reqd_quantity[i] > 0:
for j in range(0, len(gems_list)):
if gems_list[j] == reqd_gems[i]:
price = price_list[j]
break
bill_amount = bill_amount + (price) * reqd_quantity[i]
bill_amount_amt = bill_amount - (5 / 100) * bill_amount
else:
bill_amount = -1
break
return bill_amount
答案 0 :(得分:0)
我认为将金额与同一颗宝石的价格“匹配”的最佳方法是根据相应的列表创建两个字典。
尝试一下:
def calculate_bill_amount(gems_list, price_list, reqd_gems, reqd_quantity):
price_dict = {gems_list[i]: price_list[i] for i in range(len(gems_list))}
reqd_dict = {reqd_gems[i]: reqd_quantity[i] for i in range(len(reqd_gems))}
try:
bill = sum(price_dict[gem] * reqd_dict[gem] for gem in reqd_dict)
except KeyError:
return -1
if bill > 30000:
bill = 0.95*bill
return bill
#List of gems available in the store
gems_list=['Amber', 'Aquamarine', 'Opal', 'Topaz']
#Price of gems available in the store. gems_list and price_list have one-to-one correspondence
price_list=[4392, 1342, 8734, 6421]
#List of gems required by the customer
reqd_gems=['Amber', 'Opal', 'Topaz']
#Quantity of gems required by the customer. reqd_gems and reqd_quantity have one-to-one correspondence
reqd_quantity=[2, 1, 3]
bill_amount=calculate_bill_amount(gems_list, price_list, reqd_gems, reqd_quantity)
print(bill_amount)
答案 1 :(得分:0)
def calculate_bill_amount(gems_list, price_list, reqd_gems,reqd_quantity):
bill_amount=0
#Write your logic here
for i in range (0, len(reqd_gems)):
if((reqd_gems[i] in gems_list) and reqd_quantity[i]>0):
for j in range (0, len(gems_list)):
if(gems_list[j] == reqd_gems[i]):
price= price_list[j]
bill_amount = bill_amount+(price*reqd_quantity[i])
break
else:
bill_amount = -1
break
if(bill_amount>30000):
bill_amount = bill_amount - (5/100*bill_amount)
return bill_amount