Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this task, you are to write code that reads and analyses prices from a text file. Instructions Run the following code cell to download

For this task, you are to write code that reads and analyses prices from a text file.

Instructions

Run the following code cell to download a price list to your Google Drive. This file is a text file with one price (in dollars) per line. You can view the contents of this file yourself by clicking the link https://gist.github.com/anibali/85f5c4e8eb9881f64650822aa03aa55b

# DO NOT EDIT THIS CODE

# Download the data that we need for this task

import urllib.request

PRICES_URL = 'https://gist.githubusercontent.com/anibali/85f5c4e8eb9881f64650822aa03aa55b/raw/49adefb8a7ffa92da35e4f637cbebdd8ef53352f/prices.txt'

with open('prices.txt', 'w') as f:

f.write(urllib.request.urlopen(PRICES_URL).read().decode())

del f

Now that you have the prices file downloaded, your job is to write some Python code which reads and analyses those prices.

a) Reading the prices into a list

Firstly, you must read all of the prices from the file. Code which opens the file has been provided for you. Write code which reads from the file object prices_file, populating a list of numbers representing the prices that came from the file. Give your list variable an appropriate name.

Hint: you do not need to remove the trailing newline character from each line.

b) Analysing the prices

Next, you must write code which finds the the average price of items priced at $125 or more and find the the number of items that are prices below or above average price. You are to perform these computations by aggregating your list with a loop---do not use existing functions/methods like len, min, sum, or sort. Display the computed values to two decimal places using f-strings. Refer to the last part of Topic 4 for simple examples of aggregation using a loop.

You may assume that no item is priced $9999 or more.

Criteria

During marking we will check that your program:

Reads each line from the file. (2 marks)

Converts each line from the file to an appropriate data type. (2 marks)

Builds a list from the data read from the file, one item per line. (3 marks)

Performs aggregation using a loop and the list. Do not use existing functions or methods such as len, min, sum, or sort.

Average price over $125. (3 marks)

Number of items over $125 that are below the average price (3 marks)

Number of items over $125 that are above the average price (3 marks)

Prints output with correct formatting using f-strings, and uses two decimal places of precision for average numbers. (2 marks)

Prints output with correct formatting using f-strings, and uses two digit integer for number of items. (4 marks)

Executes as expected for example and other inputs. (3 marks)

prices_file = open('prices.txt', 'r')

# a) Read the prices here (don't forget to execute the code cell in the

# Instructions section first)

prices_file.close()

# b) Analyse the prices here

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

JDBC Database Programming With J2ee

Authors: Art Taylor

1st Edition

0130453234, 978-0130453235

More Books

Students also viewed these Databases questions