Advertisement
ollikolli

Untitled

Oct 9th, 2024 (edited)
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.19 KB | None | 0 0
  1. MAX_ITEMS = 15
  2.  
  3. def has_space(boat, item_count):
  4.     if len(boat) + item_count <= MAX_ITEMS:
  5.         return True
  6.     else:
  7.         return False
  8.     # Instead of this if-else structure, you could just write
  9.     # return len(boat) + item_count <= MAX_ITEMS
  10.  
  11. def add_to_boat(boat, item_count, item_name):
  12.     i = 0
  13.     for i in range(item_count):
  14.         boat.append(item_name)
  15.     return boat
  16.  
  17. def avg_all_items(b1, b2, mass):
  18.     item_count = len(b1) + len(b2)
  19.     if item_count == 0:
  20.         return 0
  21.     avg = mass / item_count
  22.     return avg
  23.  
  24. def main():
  25.     boat1 = []
  26.     boat2 = []
  27.     mass_boat1 = 0
  28.     mass_boat2 = 0
  29.     print("Enter items. Stop by entering a non-positive number of items.")
  30.     full_boat1 = False
  31.     full_boat2 = False
  32.     number_of_items = int(input("Enter the number of the items:\n"))
  33.  
  34.     while number_of_items > 0 and (not full_boat1 or not full_boat2):
  35.         name = input("Enter the name of the items:\n")
  36.         mass = int(input("Enter the mass of one item (kg):\n"))
  37.         if mass_boat1 <= mass_boat2 and has_space(boat1, number_of_items):
  38.             boat1 = add_to_boat(boat1, number_of_items, name)
  39.             mass_boat1 += number_of_items * mass
  40.         elif has_space(boat2, number_of_items):
  41.             boat2 = add_to_boat(boat2, number_of_items, name)
  42.             mass_boat2 += number_of_items * mass
  43.         elif has_space(boat1, number_of_items):
  44.             boat1 = add_to_boat(boat1, number_of_items, name)
  45.             mass_boat1 += number_of_items * mass
  46.         else:
  47.             print("There is not enough space for so many items.")
  48.         if len(boat1) == MAX_ITEMS:
  49.             full_boat1 = True
  50.         if len(boat2) == MAX_ITEMS:
  51.             full_boat2 = True
  52.         if not full_boat1 or not full_boat2:
  53.             number_of_items = int(input("Enter the number of the items:\n"))
  54.  
  55.     print(f"Boat number 1 has items {boat1}\nand it weighs {mass_boat1} kg.")
  56.     print(f"Boat number 2 has items {boat2}\nand it weighs {mass_boat2} kg.")
  57.     total_mass = mass_boat1 + mass_boat2
  58.     avg_item_mass = avg_all_items(boat1, boat2, total_mass)
  59.     print(f"The average weight of an item is {avg_item_mass:.1f} kg.")
  60.  
  61. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement