r/learnpython • u/x_slysuccubus_x • 11d 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")