有人可以帮助我在此Python程序中发现逻辑错误吗?

时间:2018-12-11 08:48:23

标签: python

问题: FoodCorner的家根据订单向其客户提供素食和非素食套餐。

素食套餐每盘120卢比,非素食套餐每盘150卢比。他们的非素食组合真的很有名,他们比素食组合获得更多的非素食组合订单。

除了每盘食物的费用外,还根据从餐厅到送货点的距离(以公里为单位)向顾客收取送货上门费用。运费如下:

距离(单位:公里),送货费,单位:Rs / km  前3公里Rs 0  接下来的3公里Rs 3  对于剩余的6卢比

给出食物的类型,数量(盘数)以及从餐厅到送货点的距离(以kms为单位),编写一个python程序来计算要由客户支付的最终账单金额。

以下信息必须用于检查客户提供的数据的有效性:

对于素食者,食物类型必须为“ V”,对于非素食者,食物类型必须为“ N”。 以公里为单位的距离必须大于0。 订购数量应最少为1。 如果任何输入无效,则账单金额应被视为-1。

我的解决方案:

def calculate_bill_amount(food_type,quantity_ordered,distance_in_kms):
    bill_amount=0.0
    if distance_in_kms >= 0.0 and distance_in_kms <= 3.0:
        if food_type == "V" and quantity_ordered >= 1:
            bill_amount = 120*quantity_ordered
        elif food_type =="N" and quantity_ordered >= 1:
            bill_amount = 150*quantity_ordered
        else:
            bill_amount = -1

    elif distance_in_kms > 3.0 and distance_in_kms <= 6.0:
        if food_type == "V" and quantity_ordered>=1:
            bill_amount = 120*quantity_ordered + 3*distance_in_kms
        elif food_type == "N" and quantity_ordered>=1:
            bill_amount = 150*quantity_ordered + 3*distance_in_kms
        else:
            bill_amount = -1
    elif distance_in_kms > 6.0:
        if food_type == "V" and quantity_ordered>=1:
            bill_amount = 120*quantity_ordered + 6*distance_in_kms
        elif food_type == "N" and quantity_ordered>=1:
            bill_amount = 150*quantity_ordered + 6*distance_in_kms
        else:
            bill_amount = -1
    else:
        bill_amount = -1
    return bill_amount

bill_amount=calculate_bill_amount("N",1,7.0)
print(bill_amount)

7 个答案:

答案 0 :(得分:0)

请根据每公里的费用而不是直接从距离计算运费。请找到以下代码。

defcalculate_bill_amount(食品类型,数量排序,距离_公里):

if food_type == 'V' and quantity_ordered>0:

    food_cost=120*quantity_ordered

elif food_type== 'N' and quantity_ordered>0:

    food_cost=150*quantity_ordered

else:

    return -1

if distance_in_kms<0:

    return -1

if distance_in_kms>6.0:

    delivery_charge = (distance_in_kms-6)*6.0+ 9.0

elif distance_in_kms>3.0 and distance_in_kms<=6.0:

    delivery_charge=(distance_in_kms-3)*3.0

else:

    delivery_charge=0

bill_amount=food_cost+delivery_charge

return bill_amount

bill_amount = calculate_bill_amount(“ N”,-1,7.0)

打印(帐单金额)

答案 1 :(得分:0)

首先,感谢您发布整个问题和整个解决方案。并不是每个人都是高手,是的,我们需要整个代码来了解我们的需求。在过去的一个小时中,我一直被一个相同的问题困扰,因此一直在寻找解决方案,但只能找到人们在谈论编写“较短的代码”。无论如何,我找到了解决方案,这是适用于所有测试用例的解决方案。

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)

答案 2 :(得分:0)

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) 打印(帐单金额)

答案 3 :(得分:0)

def calculate_bill_amount(food_type, quantity_ordered, distance_in_kms):
    bill_amount = 0
    vegComboPrice = 120
    nonVegComboPrice = 150



    if quantity_ordered > 0 and distance_in_kms > 0: 
        if food_type == "V":

            vegSubTotal = vegComboPrice * quantity_ordered
            if distance_in_kms <= 3:
                bill_amount = vegSubTotal
            elif distance_in_kms > 3 and distance_in_kms <= 6:
                distanceCharge = distance_in_kms - 3
                distanceCharge *= 3
                bill_amount = vegSubTotal + distanceCharge
            else:
                distanceCharge = distance_in_kms - 6
                distanceCharge *= 6
                bill_amount = vegSubTotal + distanceCharge + 9

        elif food_type == "N":
            nonVegSubTotal = nonVegComboPrice * quantity_ordered
            if distance_in_kms <= 3:
                bill_amount = nonVegSubTotal
            elif distance_in_kms > 3 and distance_in_kms <= 6:
                distanceCharge = distance_in_kms - 3
                distanceCharge *= 3
                bill_amount = nonVegSubTotal + distanceCharge
            else:
                distanceCharge = distance_in_kms - 6
                distanceCharge *= 6
                bill_amount = nonVegSubTotal + distanceCharge + 9

        else:
            bill_amount = -1
    else:
        bill_amount = -1

    return bill_amount


bill_amount = calculate_bill_amount("V",1,1)
print(bill_amount)

答案 4 :(得分:0)

def calculate_bill_amount(food_type,quantity_ordered,distance_in_kms):

    bill_amount=0

    if food_type == 'N' and quantity_ordered !=0 and distance_in_kms >0:
        bill_amount=150*quantity_ordered if distance_in_kms < 4 else 150*quantity_ordered+(distance_in_kms-3)*3 if 3 < distance_in_kms <=6 else 150*quantity_ordered+(distance_in_kms-6)*6+9
        return bill_amount

    elif food_type == 'V' and quantity_ordered !=0 and distance_in_kms >0:
        bill_amount=120*quantity_ordered if distance_in_kms < 4 else 120*quantity_ordered+(distance_in_kms-3)*3 if 3 < distance_in_kms <=6 else 120*quantity_ordered+(distance_in_kms-6)*6+9
        return bill_amount

    else:
        bill_amount=-1
        return bill_amount

bill_amount=calculate_bill_amount("n",2,8)

print(bill_amount)

答案 5 :(得分:0)

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)

答案 6 :(得分:0)

def calculate_bill_amount(food_type,quantity_ordered,distance_in_kms):
    bill_amount=0
    delivery_charge = 0 
    if((food_type=="N" or food_type=="V") and quantity_ordered >=1 and distance_in_kms > 0):
        if(distance_in_kms > 3 and distance_in_kms <=6):
            delivery_charge = (distance_in_kms-3)*3
        elif(distance_in_kms > 6):
            delivery_charge = (distance_in_kms-6)*6 + 9
            
        if(food_type == "N"):
            bill_amount = quantity_ordered*150 + delivery_charge
        elif(food_type == "V"):
            bill_amount = quantity_ordered*120 + delivery_charge
    else:
        bill_amount = -1
    

    return bill_amount

bill_amount=calculate_bill_amount("N",2,7)
print(bill_amount)