Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MAX_ITEMS = 15
- def has_space(boat, item_count):
- if len(boat) + item_count <= MAX_ITEMS:
- return True
- else:
- return False
- # Instead of this if-else structure, you could just write
- # return len(boat) + item_count <= MAX_ITEMS
- def add_to_boat(boat, item_count, item_name):
- i = 0
- for i in range(item_count):
- boat.append(item_name)
- return boat
- def avg_all_items(b1, b2, mass):
- item_count = len(b1) + len(b2)
- if item_count == 0:
- return 0
- avg = mass / item_count
- return avg
- def main():
- boat1 = []
- boat2 = []
- mass_boat1 = 0
- mass_boat2 = 0
- print("Enter items. Stop by entering a non-positive number of items.")
- full_boat1 = False
- full_boat2 = False
- number_of_items = int(input("Enter the number of the items:\n"))
- while number_of_items > 0 and (not full_boat1 or not full_boat2):
- name = input("Enter the name of the items:\n")
- mass = int(input("Enter the mass of one item (kg):\n"))
- if mass_boat1 <= mass_boat2 and has_space(boat1, number_of_items):
- boat1 = add_to_boat(boat1, number_of_items, name)
- mass_boat1 += number_of_items * mass
- elif has_space(boat2, number_of_items):
- boat2 = add_to_boat(boat2, number_of_items, name)
- mass_boat2 += number_of_items * mass
- elif has_space(boat1, number_of_items):
- boat1 = add_to_boat(boat1, number_of_items, name)
- mass_boat1 += number_of_items * mass
- else:
- print("There is not enough space for so many items.")
- if len(boat1) == MAX_ITEMS:
- full_boat1 = True
- if len(boat2) == MAX_ITEMS:
- full_boat2 = True
- if not full_boat1 or not full_boat2:
- number_of_items = int(input("Enter the number of the items:\n"))
- print(f"Boat number 1 has items {boat1}\nand it weighs {mass_boat1} kg.")
- print(f"Boat number 2 has items {boat2}\nand it weighs {mass_boat2} kg.")
- total_mass = mass_boat1 + mass_boat2
- avg_item_mass = avg_all_items(boat1, boat2, total_mass)
- print(f"The average weight of an item is {avg_item_mass:.1f} kg.")
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement