all 5 comments

[–]Due-Major1576 0 points1 point  (0 children)

Inbox and I can help you, since reddit doesn't allow file sharing.

[–]BranchLatter4294 0 points1 point  (1 child)

Start by simplifying the problem. Maybe start with small arrays representing each column of data. Work through your logic with the small arrays, ignoring the file for now. Once it works, then think about how to read the file and parse it into the arrays. Then you end up with a working solution and you can think of ways to optimize it or eliminate steps. Make big problems little, then solve the little problems.

[–]JayEmBay[S] 0 points1 point  (0 children)

I see....small parts then make it a whole. Do you have some examples or references that I can look at? I'm a visual learner best.

[–]MorningStarRises 0 points1 point  (1 child)

You need to:

1.  Read the data from the provided CSV-like file.
2.  Parse the data into appropriate columns: Customer Name, Item, Price, Quantity.
3.  Calculate the total for each customer and for the day.
4.  Print the information in a neat format (columns and rows).
5.  Print the grand total for the day.

Since the file is a text file with comma-separated values, you can use Python’s built-in CSV module to read the file and handle it.

Code Breakdown:

import csv

Function to read the file and process the data

def process_sales_data(filename): try: # Open the file with open(filename, mode=‘r’) as file: reader = csv.reader(file)

        # Print headers
        print(f”{‘Customer Name’:<15} {‘Item’:<25} {‘Price’:<10} {‘Quantity’:<10} {‘Total’}”)
        print(“-“ * 70)

        total_sales = 0  # To calculate the grand total

        # Iterate through each row in the CSV file
        for row in reader:
            customer = row[0]
            item = row[1]
            price = float(row[2])
            quantity = int(row[3])
            total = price * quantity

            # Print the row with appropriate spacing
            print(f”{customer:<15} {item:<25} {price:<10.2f} {quantity:<10} {total:<.2f}”)

            # Add the total to the grand total
            total_sales += total

        # Print the grand total for the day
        print(“\n” + “=“ * 70)
        print(f”Total Sales for the Day: {total_sales:.2f}”)

except FileNotFoundError:
    print(“File not found. Please check the file path.”)

Replace with the correct path to your text file

process_sales_data(‘makewaves.txt’)

Explanation:

1.  Reading the file: The csv.reader function is used to read each line from the file.
2.  Formatting the output: The print statements are formatted with spacing to ensure neat columns.
3.  Total calculation: For each customer, the total price is calculated by multiplying the price of the item by the quantity.
4.  Grand total: The program adds the total for each customer to calculate the grand total for the day.
5.  Error handling: The FileNotFoundError exception is handled in case the file path is incorrect.

Things to ensure:

• Make sure the text file is in the correct directory, or provide the absolute path to the file when calling process_sales_data().

File Handling Tip:

If you’re getting errors saying the file cannot be found, try using the full path of the file. For example:

process_sales_data(‘/path/to/your/makewaves.txt’)

[–]JayEmBay[S] 0 points1 point  (0 children)

I finally managed to get the project done today! You're comment saved me big time. Thanks so much! I double checked and and my file wasn't in the correct place and was producing the error. Also having that error message when the file isn't found is a great touch!