In: Computer Science
Programming Project 3
Home Sales
Program Behavior
This program will analyze real estate sales data stored in an input file. Each run of the program should analyze one file of data. The name of the input file should be read from the user.
Here is a sample run of the program as seen in the console window. User input is shown in blue:
Let's analyze those sales!
Enter the name of the file to process? sales.txt
Number of sales: 6
Total: 2176970
Average: 362828
Largest sale: Joseph Miller 610300
Smallest sale: Franklin Smith 199200
Now go sell some more houses!
Your program should conform to the prompts and behavior displayed above. Include blank lines in the output as shown.
Each line of the data file analyzed will contain the buyer's last name, the buyer's first name, and the sale price, in that order and separated by spaces. You can assume that the data file requested will exist and be in the proper format.
The data file analyzed in that example contains this data:
Cochran Daniel 274000
Smith Franklin 199200
Espinoza Miguel 252000
Miller Joseph 610300
Green Cynthia 482370
Nguyen Eric 359100
You can download that sample data file here to test your program, but your program should process any data file with a similar structure. The input file may be of any length and have any filename. You should test your program with at least one other data file that you make.
Note that the average is printed as an integer, truncating any fractional part.
Your program should include the following functions:
read_data - This function should accept a string parameter representing the input file name to process and return a list containing the data from the file. Each element in the list should itself be a list representing one sale. Each element should contain the first name, last name, and purchase price in that order (note that the first and last names are switched compared to the input file). For the example given above, the list returned by the read_data function would be:
[['Daniel', 'Cochran', 274000], ['Franklin', 'Smith', 199200], ['Miguel', 'Espinoza', 252000], ['Joseph', 'Miller', 610300], ['Cynthia', 'Green', 482370], ['Eric', 'Nguyen', 359100]]
Use a with statement and for statement to process the input file as described in the textbook.
compute_sum - This function should accept the list of sales (produced by the read_data function) as a parameter, and return a sum of all sales represented in the list.
compute_avg - This function should accept the list of sales as a parameter and return the average of all sales represented in the list. Call the compute_sum function to help with this process.
get_largest_sale - This function should accept the list of sales as a parameter and return the entry in the list with the largest sale price. For the example above, the return value would be ['Joseph', 'Miller', 610300].
get_smallest_sale - Like get_largest_sale, but returns the entry with the smallest sale price. For the example above, the return value would be ['Franklin', 'Smith', 199200].
Do NOT attempt to use the built-in functions sum, min, or max in your program. Given the structure of the data, they are not helpful in this situation.
main - This function represents the main program, which reads the file name to process from the user and, with the assistance of the other functions, produces all output. For this project, do not print output in any function other than main.
Other than the definitions of the functions described above, the only code in the module should be the following, at the end of the file:
if __name__ == '__main__':
main()
That if statement keeps your program from being run when it is initially imported into the Web-CAT test environment. But your program will run as normal in Thonny. Note that there are two underscore characters before and after name and main.
Include an appropriate docstring comment below each function header describing the function.
Do NOT use techniques or code libraries that have not been covered in this course.
Include additional hashtag comments to your program as needed to explain specific processing.
A Word about List Access
A list that contains lists as elements operates the same way that any other lists do. Just remember that each element is itself a list. Here's a list containing three elements. Each element is a list containing integers as elements:
my_list = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
So my_list[0] is the list [1, 2, 3, 4] and my_list[2] is [9, 10, 11, 12].
Since my_list[2] is a list, you could use an index to get to a particular value. For instance, my_list[2][1] is the integer value 10 (the second value in the third list in my_list).
In this project, the sales list is a list of lists. When needed, you can access a particular value (first name, last name, or sales price) from a particular element in the sales list.
Code screenshot:
sales.py
def read_data(file_name):
sales = []
with open(file_name) as file:
# For each line
for record in
file.read().split('\n'):
# Split each
line wit respect to white space
record =
record.split()
# Switch first
and last name
record[0],record[1] = record[1],record[0]
# Append record
to sales
sales.append(record)
# Return sales
return sales
def compute_sum(sales):
# Initial sum is 0
sum = 0
# For each record in sale
for record in sales:
# Add sales to sum
sum += int(record[2])
# Return sum
return sum
def compute_avg(sales):
# Average = sum/number of sales
return compute_sum(sales)//len(sales)
def get_largest_sale(sales):
# Assume first record as largest sale
max_sale = sales[0]
# For each record in sales
for record in sales:
# If current record has largest
sale than current large sale
if record[2]>max_sale[2]:
# update
max_sale
max_sale =
record
# Return the result
return max_sale[0]+' '+max_sale[1]+' '+max_sale[2]
def get_smallest_sale(sales):
# Assume first record as smallest sale
min_sale = sales[0]
# For each record in sales
for record in sales:
# If current record has largest
sale than current small sale
if record[2]<min_sale[2]:
# update
min_sale
min_sale =
record
# Return the result
return min_sale[0]+' '+min_sale[1]+' '+min_sale[2]
def main():
# Funtion calling and printing result to console
sales = read_data('data.txt')
print('Number of sales: ',len(sales))
print('Total: ',compute_sum(sales))
print('Average: ',compute_avg(sales))
print('Largest sale: ',get_largest_sale(sales))
print('Smallest sale: ',get_smallest_sale(sales))
if __name__ == '__main__':
main()
output screenshot: