为了账号安全,请及时绑定邮箱和手机立即绑定

有人能帮我找出这个 Python 程序中的逻辑错误吗?

有人能帮我找出这个 Python 程序中的逻辑错误吗?

守着星空守着你 2021-09-14 13:47:24
问题:FoodCorner 家根据订单向客户提供素食和非素食组合。素食套餐每盘售价 120 卢比,非素食套餐每盘售价 150 卢比。他们的非素食组合非常有名,因为他们的非素食组合比素食组合获得的订单更多。除了每盘食物的成本外,还根据从餐厅到送货点的距离(以公里为单位)向顾客收取送货上门费用。运费如下:距离以公里为单位,运费以每公里卢比为单位 前 3 公里为 0 卢比,接下来的 3 公里为 3 卢比,其余为 6 卢比给定食物类型、数量(盘子数量)以及从餐厅到送货点的距离(以公里为单位),编写一个 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_amountbill_amount=calculate_bill_amount("N",1,7.0)print(bill_amount)
查看完整描述

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)


查看完整回答
反对 回复 2021-09-14
?
猛跑小猪

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)


查看完整回答
反对 回复 2021-09-14
?
德玛西亚99

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)


查看完整回答
反对 回复 2021-09-14
  • 3 回答
  • 0 关注
  • 215 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信