3 回答
TA贡献2016条经验 获得超9个赞
def calculate_bill_amount(food_type,quantity_ordered,distance_in_kms):
bill_amount=0
if(food_type=="N" and quantity_ordered>=1 and distance_in_kms>0):
if(distance_in_kms>0 and distance_in_kms<=3):
bill_amount=150*quantity_ordered
elif(distance_in_kms >3 and distance_in_kms<=6)
bill_amount=150+(3*(distance_in_kms-3))*quantity_ordered
else:
bill_amount=150+(6*(distance_in_kms-6))*quantity_ordered
elif (food_type=="V" and quantity_ordered>=1 and distance_in_kms>0):
if(distance_in_kms>0 and distance_in_kms<=3):
bill_amount=120*quantity_ordered
elif(distance_in_kms>3 and distance_in_kms<=6):
bill_amount=120+(3*(distance_in_kms-3))*quantity_ordered
else:
bill_amount=120+(6*(distance_in_kms-6))*quantity_ordered
else:
bill_amount=-1
return bill_amount
bill_amount=calculate_bill_amount("N",1,7) 打印(bill_amount)
TA贡献1858条经验 获得超8个赞
def calculate_bill_amount(food_type,quantity_ordered,distance_in_kms):
bill_amount=0
#write your logic here
if((food_type =="V" or food_type=="N") and (quantity_ordered >0 and distance_in_kms>0)):
if(food_type == "V"):
bill_amount = 120 * quantity_ordered
elif(food_type =="N"):
bill_amount = 150 * quantity_ordered
else:
bill_amount=-1
if(0<distance_in_kms<=3):
bill_amount +=0
elif(3<distance_in_kms<=6):
bill_amount += 3*(distance_in_kms-3)
else:
bill_amount +=(9+6*(distance_in_kms-6))
else:
bill_amount = -1
return bill_amount
#Provide different values for food_type,quantity_ordered,distance_in_kms and test your program
bill_amount=calculate_bill_amount("N",2,7)
print(bill_amount)
TA贡献1770条经验 获得超3个赞
首先,感谢您发布整个问题和整个解决方案。不是这里的每个人都是大师,是的,我们需要完整的代码来理解我们需要什么。在过去的一个小时里,我对同一个问题感到震惊,因此正在寻找解决方案,但只能找到人们谈论编写“更短的代码”。无论如何,我想出了解决方案,这是适用于所有测试用例的解决方案。
def calculate_bill_amount(food_type,quantity_ordered,distance_in_kms):
bill_amount=0
if(distance_in_kms<=0 or quantity_ordered<1):
bill_amount=-1
else:
if(food_type=="N"):
if(distance_in_kms<=3):
bill_amount=(150*quantity_ordered)+(0*distance_in_kms)
elif(distance_in_kms>3 and distance_in_kms<=6):
bill_amount=(150*quantity_ordered)+(3*(distance_in_kms-3))
else:
bill_amount=((150*quantity_ordered)+((distance_in_kms-6)*6+9))
elif(food_type=="V"):
if(distance_in_kms<=3):
bill_amount=(120*quantity_ordered)+(0*distance_in_kms)
elif(distance_in_kms>3 and distance_in_kms<=6):
bill_amount=(120*quantity_ordered)+(3*(distance_in_kms-3))
else:
bill_amount=(120*quantity_ordered)+(9+6*(distance_in_kms-6))
else:
bill_amount=-1
return bill_amount
bill_amount=calculate_bill_amount("n",2,8)
print(bill_amount)
添加回答
举报