Reputation: 1
I have the following data in a text file on which we will be working.
Oct 26th 01:12 pm
» corwinte: Ashar Delivery (64) to Rhad / Set Fee: 31895 CR Oct 26th 01:12 pm
» corwinte: Ashar Delivery (32) to Habitat-01 / Set Fee: 15716 CR Oct 24th 04:37 pm
» Dagon hammersmith: Ashar Delivery (23) to Lus / Set Fee: 11871 CR Oct 24th 04:33 pm
» Dragon: Ashar Delivery (74) to Enala / Set Fee: 38192 CR Oct 24th 04:33 pm
» Dagon hammersmith: Ashar Delivery (54) to Enala / Set Fee: 27870 CR Oct 24th 04:33 pm
» Dragon: Ashar Delivery (45) to Turroth / Set Fee: 23225 CR Oct 24th 04:33 pm
» Falconner: Ashar Delivery (38) to Ceas / Set Fee: 19612 CR
I am just learning python,so here is my rough code,it does not do all I want it to do,but it does some,and somethings not properly.
First, we are not interested in lines that begin with dates so we just ignore those. What I want is,for the code to go through the text files,gather all the names along with their credits(CR) values,calculate a percentage of each and store the total with the name.
So far, I have been able to ask the percentage,gather the names and get percentage of all entrys,not total.
Here is my code.
inp = input('enter the name of the file to be opened\n') #asks for a file,opens it,a guard to handle misspelled or files that don't exist
try:
fhand = open(inp)
except:
print('Are you on medications? there is no',inp)
exit()
per = input('Enter the percentage you would like to calculate from,percentage of the fees recieved')
perc = float(per)
for line in fhand:
if not line.startswith('»'): continue
words = line.rstrip().split() #stripping the right spaces and splitting into words
feesrecieved = words[-2]
actam = float(feesrecieved)*perc/100
print(words[1], actam)
How do I get it to say,give me total calculated value of each name? I.E all dragons hauls,all dagons hauls and so on. Thank you!
Upvotes: 0
Views: 40
Reputation: 31
I think I understand your question. I would add it to a dict and update the values as I go along.
per = input('Enter the percentage you would like to calculate
from,percentage of the fees recieved')
perc = float(per)
totalFees = {} // create empty dict
for line in fhand:
if not line.startswith('»'): continue
words = line.rstrip().split() #stripping the right spaces and splitting into words
feesrecieved = words[-2]
actam = float(feesrecieved)*perc/100
print(words[1], actam)
totalFees[words[1]] = totalFees.get([words[1], 0) + actam //updating dict
you can either add after finding the percentage or before and caculate the total at the end
Upvotes: 1