Reputation: 23
I have a text file like this:
month /name/ number/ price
1 John 100 120.00
1 Sean 90 125.00
1 Laura 150 100.00
1 Joseph 95 140.00
1 Pam 91 105.00
2 John 110 120.00
2 Sean 98 100.00
2 Laura 100 100.00
2 Joseph 89 150.00
2 Pam 100 100.00
3 John 100 121.00
3 Sean 90 120.00
3 Laura 97 100.00
3 Joseph 120 110.00
3 Pam 101 100.00
I need to get a specific person's (such as Pam) revenue per month and total revenue in 1,2 and 3 months (number*price). I have the code below and the output below. But I have no idea how to get the total revenue, can anyone give to me some advice or idea?
#This is the code I use
f = input('Enter The File Name:')
sales_data = open("sales.txt",'r')
lines = sales_data.readlines()
m = input('Enter the Manager Name:')
print('Monthly Sales Report for' +' ' + m)
for line in lines:
line = line.split()
tr = (float(line[2]) * float(line[3]))
if m in line:
print(line[0] +' ' + line[2] + ' ' + line[3] +' ' + str(tr))
#This is the output I got
Enter the Manager Name: Pam
Monthly Sales Report for Pam
1 91 105.00 9555.0
2 100 100.00 10000.0
3 101 100.00 10100.0
Upvotes: 2
Views: 518
Reputation: 2901
f = input('Enter The File Name: ')
sales_data = open(f,'r')
lines = sales_data.readlines()
m = input('Enter the Manager Name: ')
print('Monthly Sales Report for ' + m)
TOTAL_REVENUE=0
for line in lines:
line = line.split()
if m in line:
tr = (float(line[2]) * float(line[3]))
TOTAL_REVENUE=TOTAL_REVENUE+tr
print(line[0] +' ' + line[2] + ' ' + line[3] +' ' + str(tr))
print("GRAND TOTAL REVENUE: " + str(TOTAL_REVENUE))
Upvotes: 0
Reputation: 4744
One possible way to solve your issue is to store all monthly values in a dictionary for a particular manager:
file_name = input('Enter The File Name: ')
manager_summary = {'1':0.0, '2':0.0, '3':0.0}
with open (file_name, 'r') as fin:
lines = fin.readlines()
manager = input('Enter the Manager Name: ')
print('Monthly Sales Report for' +' ' + manager)
for line in lines:
line = line.split()
if manager in line:
manager_summary[line[0]] += float(line[-2])*float(line[-1])
manager_total = 0.0
for key, value in manager_summary.items():
manager_total += value
print(manager_total)
The code reads the input file at once, loops through all the lines in search of the target manager and stores cumulative monthly sales for that manager in a dictionary. The total revenue for 3 month period is then computed by adding cumulative values for each month stored in the dictionary.
There were couple changes with respect to your original code worth noting:
open
this code uses with open
- with open is a context manager that will automatically close the file for your when closing is needed, something that your were missing in your program.f
, m
, it makes the program more bug prone and way less readable. The ones used here are longish, you can always come up with something inbetween.Upvotes: 1
Reputation: 904
You can solve this using a dictionary - specifically a defaultdict
. You can keep track of a dictionary of people's names to revenue.
First import defaultdict
and define a dictionary:
from collections import defaultdict
revenue_dictionary = defaultdict(float)
Then just after you've calculated tr
, add this to the dictionary:
revenue_dictionary[line[1]] += tr
At the end of the script, you'll have a dictionary which looks like:
{
'John': 37300.0,
'Sean': 31850.0,
'Laura': 34700.0,
'Joseph': 39850.0,
'Pam': 29655.0
}
And you can access any of these using revenue_dictionary['Pam']
, or m
instead of 'Pam'
.
Upvotes: 0