r/learnpython • u/x_slysuccubus_x • 20d ago
I have an assignment and I am so confused
i talked to 4 tutors and for some reason i can not get this to work after 2 hours with a tutor. please tell me what i am doing wrong!
b. A function named DisplayCardSales that displays scores for all items in the text file provided. Each score should have a line number to the left starting at 1. For example: 1: 1.24 2: 0.97. (see output example below). This function accepts zero arguments and does not return any values. This function does read from a text file, so it needs error handling for FileNotFoundError.
c. A function named CalcTotal that reads the text file provided, accumulates a total and divides the total by the number of items in the file provided. This function accepts zero arguments and returns two values. The values returned are the grand total and the sales amount as a result. This function does read from a text file, so it needs error handling for FileNotFoundErrors and ValueErrors.
def CalcTotal():
try:
infile = open("cards.txt",'r')
except:
print("Error")
else:
count = 0
totalsales = 0
for line in infile:
sales = float(line)
totalsales = totalsales + sales
count = count + 1
avgsale = totalsales/sales
print("Total Sales: $", totalsales)
print("Average Sale: $", avgsale)
infile.close()
def DisplayCardSales():
try:
infile = open("cards.txt",'r')
i = 0
for line in infile:
print((i+1), ": $", line)
i = (i + 1)
infile.close()
except:
print("Error")
14
u/atarivcs 20d ago
What exactly do you mean when you say "can not get this to work"?
Do you mean the code does not run because it has errors?
Do you mean the code runs, but produces no output?
Do you mean the code runs, but produces incorrect output?
3
u/Agawell 20d ago
Adding some extra print statements will help massively in debugging - ie print out each line that’s being read from each of the files - so you know the file is being read correctly
You can comment them out or delete them once you’ve verified what’s happening!
What is actually being output when you call these functions?
And what are your expected results?
3
u/Western_Progress_456 20d ago
avgsales=totalsalles/count
1
u/Atmosck 20d ago
I second the question about what you mean by it not working. If it's producing an error, it would helpful to see the error message and traceback. If it's being rejected by your teacher or some online grading system, is there any feedback?
Your DisplayCardSales look likes it should work. Did they say what it should do if there is an error? Normally if you're catching an error it's because you want the program to do something other than crash when you get an error.
Your CalcTotal function doesn't return anything, like the question asks. What does the file look like? Your code treats it like it's just a single number on each line, each representing one s ale. In which case, your avgsale line should divide by count, not sales. But I would expect the file to look more like a csv with multiple things per row, like:
In which case you would want to split each row, like
line.split(",")In both questions, the wording makes it seem like they want you to catch those specific error types. In problem C they probably want different results depending on which error type.
In general, bare
exceptis considered bad practice because it catches everything. Normally you want to catch only the errors you expect, and let other error go uncaught. Like if you're reading a file and it's not there, you can print a message saying what's wrong. But if you're reading a file and get some other error like you ran out of memory, that's considered "unrecoverable" and the program should actually crash in that case.1
u/WorriedTumbleweed289 20d ago
I would return after print error and unnest the else clause. In the for loop, use print statments for the line and sales to debug line by line to see what is going on.
1
u/Chorizo_n_eggs 20d ago
Not seeing any call to the functions. Not seeing any return in CalcTotal. You said it returns two values
Do you get an error?
Is this all the code?
2
u/Agawell 20d ago
Are you actually calling the functions?
1
u/Lion2471 20d ago
CalcTotal is supposed to return two values. Did you do that?
1
u/RngdZed 20d ago
on top of what everyone else said, id use with open as a standard of opening files. also theres a weird indent after for line in infile: .. it has only 2 spaces indent, it wont affect the code running, but it's not proper pep8, sorry to bother you with that one lol.. your IDE should yell at you for inconsistent indentation, unless its a copy paste error idk
1
u/Discordbothost 20d ago
one concrete bug in CalcTotal: avgsale = totalsales/sales divides by the last line's price, not by how many lines you read. use count instead:
avgsale = totalsales / count
also that bare except: print("Error") is why you only see "Error" — cards.txt is probably not in the cwd when you run it. temporarily drop the try/except or do
except Exception as e: print(e)so you see FileNotFoundError vs ValueError.1
u/throwaway6560192 20d ago
Remove the
try: ... except: print("Error")stuff. It's only obscuring the true error. Then run it and read the error message.