IF/ELSE用Python控制流,从4个选项中计算出快递服务的价格
我似乎无法理解如何在以下问题中使用if/else:IF/ELSE用Python控制流,从4个选项中计算出快递服务的价格,python,if-statement,controls,flow,Python,If Statement,Controls,Flow,我似乎无法理解如何在以下问题中使用if/else: 您需要为快递公司设计一个程序来计算发送包裹的成本 要求用户输入他们想要购买的软件包的价格 要求用户输入交付的总距离(以公里为单位) 现在,加上交付成本,得到产品的最终成本。在确定包裹的最终成本时,有四个类别需要考虑,每个类别都有两个基于客户交付偏好的选项。(根据他们的选择使用if-else语句) 空运(每公里0.36美元)或货运(每公里0.25美元) 全额保险($50.00)或有限保险($25.00) 礼品选项(15.00美元)与否(0.00美
#快递包裹的费用
#您可以也应该添加自己的断言,并检查用户输入是否有效
#我使用了一个列表而不是“==”,这样您可以扩展响应选项
#还有很多其他的方法,这只是我快速而肮脏的方法
#但我想你可以从这里迭代
def user_input():
包的价格=浮动(输入('包的输入价格。\n'))
总距离=浮动(输入('input total distance in km\n'))
运费或空运=输入('输入运费或空运?\n')。下限()
完全保险或有限保险=输入('输入完全保险或有限保险?\n')。下限()
gift\u或\u not=输入('这是礼物吗?\n')。lower()
优先级\或\标准=输入('这是优先级还是标准交付?\n')。下限()
每距离成本=0
如果运费或空运在[‘运费’]:
每公里成本=0.25
elif货运或空运[‘空运’]:
每公里成本=0.36
保险成本=0
如果全额保险或有限保险在[‘全额’]:
保险费=50.00
elif全险或有限险在['limited']:
保险费用=25.00
礼物的成本=0
如果礼物不在[“是”]:
礼物的成本=15
交付成本=0
如果[‘优先级’]中的优先级或标准:
交付成本=100
elif优先级或[“标准”中的标准:
交付成本=20
打印(f'\n用户已指定\n\
包的价格:{price\u of_package}\n\
总距离:{total_distance}\n\
货运或空运:{货运或空运}\n\
每距离成本{每距离成本}\n\
保险类型:{全额保险或有限保险}\n\
保险费:{cost_per_distance}\n\
如果是礼物:{gift\u或\u not}\n\
礼品成本:{礼品成本}\n\
传递类型:{priority\u或\u standard}\n\
交付成本:{交付成本}.)
返回包装的价格、总距离、每距离成本、\
保险费、礼品费、送货费
def总成本():
包装价格、总距离、每距离成本、\
保险成本、礼品成本、配送成本=用户输入()
总成本=包装价格+总距离*每距离成本+\
保险费+礼品费+送货费
打印(f'\n总成本为{total_cost}.)
返回总成本
@mohsee-Ramjan
我使用了一些原始代码,但简化了一点。我不是专家,我相信即使是这段代码也可以改进很多 ====注:我们的货币是兰特,因此使用“R”===== 但也许你会更了解这一点:
main_product_price = int(float(input("""Please enter the price of the package you would like to purchase:
(Only use numbers, do not inlcude 'R')\n""")))
total_distance = int(float(input("\nPlease enter the total distance of the delivery in Kilometers:\n")))
print ("\nNow please choose your shipping preferences;")
print ("\nWould you prefere Air at R0.36 per km, or Freight at R0.25 per km?")
freight_choise = input("Enter: 'Air' or 'Freight'\n")
cost_per_distance = 0
if freight_choise in ['Freight']:
cost_per_distance = 0.25
elif freight_choise in ['Air']:
cost_per_distance = 0.36
print ("\nWould you prefere Full insurance (R50.00), or Limited insurance (R25.00)?")
insurance_choise = input("Enter: 'Full' or 'Limited'?\n")
insurance_cost = 0
if insurance_choise in ['Full']:
insurance_cost = 50
elif insurance_choise in ['Limited']:
insurance_cost = 25
print ("\nWould you like to add a Gift for R15.00?")
gift_choise = input("Enter: 'Yes please' or 'No thanks'\n")
gift_cost = 0
if gift_choise in ['Yes please']:
gift_cost = 15
elif gift_choise in ['No thanks']:
gift_cost = 0
print ("\nWould you prefere Priority delivery for R100.00, or Standard delivery for R20.00?")
delivery_choise = input("Enter: 'Priority' or 'Standard'\n")
priority_or_standard_delivery = 0
if delivery_choise in ['Priority']:
priority_or_standard_delivery = 100
elif delivery_choise in ['Standard']:
priority_or_standard_delivery = 20
total_cost = main_product_price + total_distance*cost_per_distance + insurance_cost + gift_cost + priority_or_standard_delivery
print (f"""\nThis is your Total cost:
R{total_cost}""")
到目前为止你试过什么?你在找蟒蛇吗?谢谢你的回复。它非常先进,我无法理解。我应该只获取用户输入,然后使用if/else来解决问题。尚未达到创建自定义函数或使用elif的阶段。无论如何,谢谢你。