IF / ELSE Контроль потока в Python для расчета цены курьерской услуги из 4 вариантов - PullRequest
0 голосов
/ 31 октября 2019

Я не могу понять, как использовать if / else в следующем вопросе:

  1. Вам необходимо разработать программу для курьерской компании, чтобы рассчитать стоимость отправки посылки.
  2. Попросите пользователя ввести цену пакета, который он хотел бы приобрести.
  3. Попросите пользователя ввести общее расстояние доставки в километрах.
  4. Теперь добавьте кстоимость доставки, чтобы получить окончательную стоимость продукта. При определении окончательной стоимости посылки необходимо учитывать четыре категории, каждая из которых имеет два варианта в зависимости от предпочтений клиента. (Используйте оператор if else, основываясь на сделанном ими выборе)
  5. Доставка по воздуху (0,36 долл. США за км) или грузовым транспортом (0,25 долл. США за км)
  6. Полная страховка (50,00 долл. США) или ограниченная страховка (25,00 долл. США
  7. Вариант подарка (15,00 долл. США) или нет (0,00 долл. США)
  8. Приоритетная доставка (100,00 долл. США) или стандартная доставка (20,00 долл. США)
  9. Введите код для расчета общей стоимости:пакет, основанный на опциях, выбранных в каждой категории.

Может кто-нибудь помочь? Спасибо.

1 Ответ

0 голосов
/ 31 октября 2019
# Courier cost of delivering parcel
# You can and should add your own assertions and checks if the user input is valid
# I used a list instead of '==' so that you can expand the response options
# There are many other ways to do it, this is just my quick and dirty method
# But I suppose you could iterate from here

def user_input():
  price_of_package = float(input('Input price of package.\n'))
  total_distance = float(input('Input total distance in km\n'))
  freight_or_air = input('Input freight or air delivery?\n').lower()
  full_or_limited_insurance = input('Input full or limited insurance?\n').lower()
  gift_or_not = input('Is this a gift?\n').lower()
  priority_or_standard = input('Is this priority or standard delivery?\n').lower()

  cost_per_distance = 0
  if freight_or_air in ['freight']:
    cost_per_distance = 0.25
  elif freight_or_air in ['air']:
    cost_per_distance = 0.36

  cost_of_insurance = 0
  if full_or_limited_insurance in ['full']:
    cost_of_insurance = 50.00
  elif full_or_limited_insurance in ['limited']:
    cost_of_insurance = 25.00

  cost_of_gift = 0
  if gift_or_not in ['yes']:
    cost_of_gift = 15

  cost_of_delivery = 0
  if priority_or_standard in ['priority']:
    cost_of_delivery = 100
  elif priority_or_standard in ['standard']:
    cost_of_delivery = 20

  print (f'\nThe user has specified that\n\
           price of package: {price_of_package}\n\
           total distance: {total_distance}\n\
           freight or air: {freight_or_air}\n\
           cost per distance {cost_per_distance}\n\
           type of insurance: {full_or_limited_insurance}\n\
           cost of insurance: {cost_per_distance}\n\
           if it is a gift: {gift_or_not}\n\
           cost of gift: {cost_of_gift}\n\
           type of delivery: {priority_or_standard}\n\
           cost of delivery: {cost_of_delivery}.')

  return price_of_package, total_distance, cost_per_distance,\
         cost_of_insurance, cost_of_gift, cost_of_delivery

def total_cost():
  price_of_package, total_distance, cost_per_distance,\
  cost_of_insurance, cost_of_gift, cost_of_delivery = user_input()

  total_cost = price_of_package + total_distance*cost_per_distance +\
               cost_of_insurance + cost_of_gift + cost_of_delivery

  print (f'\nThe total cost is {total_cost}.')
  return total_cost
...