×
Reviews 4.9/5 Order Now

Create A Program to Calculate Food Density in The Python Assignment Solution

June 29, 2024
Dr. Matthew Hernandez
Dr. Matthew
🇨🇭 Switzerland
Python
Dr. Matthew Hernandez, an esteemed Computer Science researcher, obtained his PhD from ETH Zurich, Switzerland. With 6 years of experience under his belt, he has successfully completed over 400 Python assignments, demonstrating his proficiency and commitment to excellence.
Key Topics
  • Instructions
    • Objective
  • Requirements and Specifications
Tip of the day
Use object-oriented programming principles like encapsulation and inheritance effectively. Manage memory wisely with smart pointers (std::unique_ptr, std::shared_ptr) to prevent leaks. Always compile with warnings enabled (-Wall -Wextra) and use debugging tools like GDB or Valgrind for troubleshooting.
News
Tauri v2.2.5 Update: Tauri, a framework for building cross-platform applications using web technologies, released version 2.2.5 in January 2025, enhancing performance and security.

Instructions

Objective

Write a python assignment program to calculate food density.

Requirements and Specifications

Given a text file of data about fruit with three columns of data: mass (float), volume (float) and type (string, e.g., "apple" or “orange”). Read in the data and calculate the average density of the apples and oranges.

Source Code

def read_data(filename): f = open(filename, 'r') data = [] lines = f.read().splitlines()[1:] for line in lines: parts = line.split(',') data.append([parts[0], float(parts[1]), float(parts[2])]) f.close() return data def main(): total_volume = {'apple': 0.0, 'orange': 0.0} total_mass = {'apple': 0.0, 'orange': 0.0} for rec in read_data('fruit.csv'): if rec[0] in total_mass: total_mass[rec[0]] += rec[1] total_volume[rec[0]] += rec[2] print('Apple density:', (total_mass['apple']/total_volume['apple'])) print('Orange density:', (total_mass['orange']/total_volume['orange'])) if __name__ == '__main__': main()

Similar Samples

"Explore our diverse range of programming homework samples at ProgrammingHomeworkHelp.com. From introductory exercises to complex projects in Java, Python, C++, and more, our samples demonstrate our expertise in solving various programming challenges. These examples showcase the quality and depth of our services, providing insights into how we can assist you with your programming assignments.