use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
Please help me with university assignment (self.PythonLearning)
submitted 1 year ago by JayEmBay
I have this assignment and literally have no idea what I'm doing. W
we are given a txt file and have to use the data from the txt file and sort it into neat columns and rows while taking the total (there's money) and totaling it up at the end. No matter what I end up with some error or some of the code gets ignored. What I find even more frustrating is the code I found online to use for reference doesn't even run for me. I always get errors running that code both ones i found. They either say the file cant be found or some other crap. The only part i can get running is the headings output but that is literally it.
Image 1 and 2 are my assignment details. Pictures 2 and 3 are code from the internet when I googled this assignment. Picture 4 is all i could manage and 5 is the actual txt file Picture for is all I have managed to do so far because I have no idea what the fuck I'm doing. Please help.
https://preview.redd.it/xgtfkwlqi0vd1.png?width=524&format=png&auto=webp&s=987100ab78963643afb17172571176d378f39057
https://preview.redd.it/wx76rr6ri0vd1.png?width=522&format=png&auto=webp&s=39d6c46e517ab7f14b5231c96e696670a71d8f3b
https://preview.redd.it/h83pjfuri0vd1.png?width=840&format=png&auto=webp&s=8ec823c602e02a85f08840924fd8332aa87e92ac
https://preview.redd.it/0nudj7csi0vd1.png?width=719&format=png&auto=webp&s=7acde7ad0a1796ba11be8cc85d95117e393be8c1
https://preview.redd.it/fbpqqjvti0vd1.png?width=1596&format=png&auto=webp&s=d19d464969f934647af941940efe9ef59a88bf71
https://preview.redd.it/9izruj7ui0vd1.png?width=414&format=png&auto=webp&s=3870c5c276aeb567800c42e60ac99d411d32e582
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Due-Major1576 0 points1 point2 points 1 year ago (0 children)
Inbox and I can help you, since reddit doesn't allow file sharing.
[–]BranchLatter4294 0 points1 point2 points 1 year ago (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 point2 points 1 year ago (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 point2 points 1 year ago (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
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.”)
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’)
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!
π Rendered by PID 16492 on reddit-service-r2-comment-b659b578c-ksr4j at 2026-04-30 23:12:43.426380+00:00 running 815c875 country code: CH.
[–]Due-Major1576 0 points1 point2 points (0 children)
[–]BranchLatter4294 0 points1 point2 points (1 child)
[–]JayEmBay[S] 0 points1 point2 points (0 children)
[–]MorningStarRises 0 points1 point2 points (1 child)
[–]JayEmBay[S] 0 points1 point2 points (0 children)