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

问题: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_amount


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

print(bill_amount)


守着星空守着你
浏览 216回答 3
3回答

慕沐林林

def calculate_bill_amount(food_type,quantity_ordered,distance_in_kms):&nbsp; &nbsp; bill_amount=0&nbsp; &nbsp; if(food_type=="N" and quantity_ordered>=1 and distance_in_kms>0):&nbsp; &nbsp; &nbsp; &nbsp; if(distance_in_kms>0 and distance_in_kms<=3):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bill_amount=150*quantity_ordered&nbsp; &nbsp; &nbsp; &nbsp; elif(distance_in_kms >3 and distance_in_kms<=6)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bill_amount=150+(3*(distance_in_kms-3))*quantity_ordered&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bill_amount=150+(6*(distance_in_kms-6))*quantity_ordered&nbsp; &nbsp; elif (food_type=="V" and quantity_ordered>=1 and distance_in_kms>0):&nbsp; &nbsp; &nbsp; &nbsp; if(distance_in_kms>0 and distance_in_kms<=3):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bill_amount=120*quantity_ordered&nbsp; &nbsp; &nbsp; &nbsp; elif(distance_in_kms>3 and distance_in_kms<=6):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bill_amount=120+(3*(distance_in_kms-3))*quantity_ordered&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bill_amount=120+(6*(distance_in_kms-6))*quantity_ordered&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; bill_amount=-1return bill_amountbill_amount=calculate_bill_amount("N",1,7) 打印(bill_amount)

猛跑小猪

def calculate_bill_amount(food_type,quantity_ordered,distance_in_kms):&nbsp; &nbsp; bill_amount=0&nbsp; &nbsp; #write your logic here&nbsp; &nbsp; if((food_type =="V" or&nbsp; food_type=="N") and (quantity_ordered >0 and distance_in_kms>0)):&nbsp; &nbsp; &nbsp; &nbsp; if(food_type == "V"):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bill_amount = 120 * quantity_ordered&nbsp; &nbsp; &nbsp; &nbsp; elif(food_type =="N"):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bill_amount = 150 * quantity_ordered&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bill_amount=-1&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if(0<distance_in_kms<=3):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bill_amount +=0&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; elif(3<distance_in_kms<=6):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bill_amount += 3*(distance_in_kms-3)&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bill_amount +=(9+6*(distance_in_kms-6))&nbsp;&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; bill_amount = -1&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; return bill_amount#Provide different values for food_type,quantity_ordered,distance_in_kms and test your programbill_amount=calculate_bill_amount("N",2,7)print(bill_amount)

德玛西亚99

首先,感谢您发布整个问题和整个解决方案。不是这里的每个人都是大师,是的,我们需要完整的代码来理解我们需要什么。在过去的一个小时里,我对同一个问题感到震惊,因此正在寻找解决方案,但只能找到人们谈论编写“更短的代码”。无论如何,我想出了解决方案,这是适用于所有测试用例的解决方案。def calculate_bill_amount(food_type,quantity_ordered,distance_in_kms):&nbsp; &nbsp; bill_amount=0&nbsp; &nbsp; if(distance_in_kms<=0 or quantity_ordered<1):&nbsp; &nbsp; &nbsp; &nbsp; bill_amount=-1&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; if(food_type=="N"):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(distance_in_kms<=3):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bill_amount=(150*quantity_ordered)+(0*distance_in_kms)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif(distance_in_kms>3 and distance_in_kms<=6):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bill_amount=(150*quantity_ordered)+(3*(distance_in_kms-3))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bill_amount=((150*quantity_ordered)+((distance_in_kms-6)*6+9))&nbsp; &nbsp; &nbsp; &nbsp; elif(food_type=="V"):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(distance_in_kms<=3):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bill_amount=(120*quantity_ordered)+(0*distance_in_kms)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif(distance_in_kms>3 and distance_in_kms<=6):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bill_amount=(120*quantity_ordered)+(3*(distance_in_kms-3))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bill_amount=(120*quantity_ordered)+(9+6*(distance_in_kms-6))&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bill_amount=-1&nbsp; &nbsp; return bill_amountbill_amount=calculate_bill_amount("n",2,8)print(bill_amount)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python