Question

(Python)

Write a program called sales.py that uses a list to hold the sums of the sales for each month. The list will have 12 items with index 0 corresponds with month of “Jan”, index 1 with month “Feb”, etc.

o Write a function called Convert() which is passed a string which is a month name (ex. “Jan”) and returns the matching index (ex. 0). Hint – use a list of strings to hold the months in the correct order, e.g., months = [‘Jan’, ‘Feb’, …]

o Month Name Index

Jan 0

Feb 1

Mar 2

Apr 3

May 4

Jun 5

Jul 6

Aug 7  

Sep 8

Oct 9

Nov 10

Dec 11

o Download the text file sales.txt from https://drive.google.com/open?id=1jCqinXsf2wyh_xo8XjBXxsjB1SYwtgf7 and put it in your PyCharm project folder. Open and read the file which contains an unknown number of data sets. Each data set contains a month name on one line and the sales for the month on the second line. Because of this format, reading the file using a simple for loop can be tricky (because you have to read two lines to obtain one record). As a result you may want to call the readline() function and use a while loop (calling the readline() function twice). Note – readline() does not strip off the newline character at the end of the line, so you will need to strip this off your string (using the replace(‘\n’, ‘’) function).

o Using a list with 12 floating point numbers to accumulate your sales by month. As you read each pair of data, call the Convert() function to index the list and update the correct sale sum in the lists of sums by month. Compute the grand total of all sales.

o After you’ve read all the records, then use a for loop to iterate over the months and report the month number and the sums for each month. Use the string.format() function to format zones to align data using 2 decimal places on the monetary figures. Next report the grand total with currency format and 2 decimal places.

o The output of the program will look as follows:

COS-120 [C:\Usersl michal PycharmProjects\COS-120] -sale... File Edit View Navigate Code Refactor Run Iools VCS Window Help coS-120sales.py salesQ t Run: sales x C: Users mlcha \venvicos-1201Scripts python.exe C:/U Month Sum $800.00 $500.00 $800.00 $1,200.00 $500.00 $1,700.00 $1,400.00 $1,700.00 $1,200.00 $1,700.00 $0.00 $4,100.00 Jan Feb Mar Apr May Jun İ Jul Aug Sep oct NOV Dec Grand total is: $15, 600.00 Process finished with exit code 0 4: Run6: TODO Terminal Python Console Event Log 27:45 CRLFUTF-8

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#code screenshot

nths [ . Jan Feb Mar , Apr May Jun , Ju1 , Aug , Sep , Oct , Nov, Dec def convert (3): #convert function implementation i-0 while i<len (months) if (months [i]S) return i i+ 1 return -1 #returns -1 if s is not in months list f- open(sales.txt) #opens sales.txt file values[0,0,0,0,0,0,0,0,0,0,0,01 total- 0.0 while True: #populates the values list and adds the values to total s-f.readline.replace(In,) if s-: break index -convert (s) s- f.readline () if break val-s.replace (n,) values [index] +float (val) Sum) print (Month i#0 #prints list values to the console using loop while i<len (months): $%7 . 2 f %values [1] ) print (months [i], total +values [i] print ( \nGr and Total is: $1. 2 f %total)

#code

months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
  
def convert(s): #convert function implementation
i=0
while i<len(months):
if(months[i]==s):
return i
i += 1
return -1 #returns -1 if s is not in months list

f = open("sales.txt") #opens sales.txt file
values = [0,0,0,0,0,0,0,0,0,0,0,0]
total = 0.0
while True: #populates the values list and adds the values to total
s = f.readline().replace('\n','')
if s=="":
break
index = convert(s)
s = f.readline()
if s=="":
break
val = s.replace('\n','')
values[index] += float(val)

print('Month Sum')
i=0
#prints list values to the console using loop
while i<len(months):
print(months[i],' $%7.2f'%values[i])
total += values[i]
i+=1
print('\nGrand Total is: $%.2f'%total)

#output screenshot

Add a comment
Know the answer?
Add Answer to:
(Python) Write a program called sales.py that uses a list to hold the sums of the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider five years of monthly sales data for a company in the attached file (Q5.xlsx). a) Foreca...

    Consider five years of monthly sales data for a company in the attached file (Q5.xlsx). a) Forecast monthly sales for the next year (2020). (10 marks) b) Discuss whether a simple exponential smoothing model works well with this data or not. (5 m 7 9 Month Sales 747 Feb-14 697 Mar-14 1014 Jan-14 Ap4 1126 May-14 1105 Jun-14 1450 Jul-14 1633 Aug-14 1711 Sep-14 1307 Oct-1223 Nov-14 9T5 Dec-14S53 4J-15 1024 Feb-15928 Mar-151442 7Apr-151371 May-15 1536 Ju15 2004 Jul-15 1854...

  • Python program - Write a Python program, in a file called sortList.py, which, given a list...

    Python program - Write a Python program, in a file called sortList.py, which, given a list of names, sorts the names into alphabetical order. Use a one dimensional array to hold the list of names. To do the sorting use a simple sorting algorithm that repeatedly takes an element from the unsorted list and puts it in alphabetical order within the same list. Initially the entire list is unsorted. As each element is placed in alphabetical order, the elements in...

  • In MATLAB: 1) For the functions fscanf and textscan, write a script for each function to...

    In MATLAB: 1) For the functions fscanf and textscan, write a script for each function to compare them. The first script will read the DATA.txt file by using fscanf and then plot each temperature data monthly. The second script, which reads DATA.txt file but uses the textscan function, and plot each temperature data monthly. DATA.txt ave.temp ave.high.temp ave.low.temp highest.rec.temp lowest.rec.temp Jan 31.60 38.60 24.60 69.00 -4.00 Feb 31.70 39.20 24.30 69.00 -11.00 Mar 39.00 47.10 31.10 83.00 7.00 Apr 48.90...

  • Write a program that uses a two-dimensional array to store the highest and lowest temperatures for...

    Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. You should also have a parallel array to store the names of the 12 months. The program should read the Month's names, and the temperatures from a given input file called temperature.txt. The program should output the highest and lowest temperatures for the year, and the months that correspond to those temperatures. The program must use the following functions:...

  • Assignment You will be developing a speeding ticket fee calculator. This program will ask for a t...

    Assignment You will be developing a speeding ticket fee calculator. This program will ask for a ticket file, which is produced by a central police database, and your program will output to a file or to the console depending on the command line arguments. Furthermore, your program will restrict the output to the starting and ending dates given by the user. The ticket fee is calculated using four multipliers, depending on the type of road the ticket was issued: Interstate...

  • IN PYTHON Implement a function printSeqs() that accepts a list of name lst and prints the...

    IN PYTHON Implement a function printSeqs() that accepts a list of name lst and prints the following sequences. The sequences are printed by the for loops found in the function. The information below shows how you would call the function printSeqs() and what it would display: Write a for loop that iterates through a list of names and print a greeting for each name in the list. Request from the user a positive integer n and prints all the positive...

  • In this stage you should read all of the data into internal structures suitable for use in the later stages and create...

    In this stage you should read all of the data into internal structures suitable for use in the later stages and create an output representation that provides an overview of the data that was read. Photo 1&2 - Question Photo 3 - Input data Photo 4 - Introduction please write a code that will execute such function & generate a required output (as stated on the question). Input data is not required to write a code but if you need...

  • Program 7 File Processing and Arrays (100 points) Overview: For this assignment, write a program that...

    Program 7 File Processing and Arrays (100 points) Overview: For this assignment, write a program that will process monthly sales data for a small company. The data will be used to calculate total sales for each month in a year. The monthly sales totals will be needed for later processing, so it will be stored in an array. Basic Logic for main() Note: all of the functions mentioned in this logic are described below. Declare an array of 12 float/doubles...

  • Using the program segment and sample txt file below, write a program that contains a linked...

    Using the program segment and sample txt file below, write a program that contains a linked list of students. The program should allow the user to: 1. initialize list of students 2. add additional student to front of list 3. add additional student to rear of list 4. delete student 5. sort students alphabetically 6. sort students by idNum 7. show number of students in list 8. print students 9. quit program XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX The program should be divided into the...

  • -----------Python program------------------- Instructions: For this assignment, you will write complete a program that allows a customer...

    -----------Python program------------------- Instructions: For this assignment, you will write complete a program that allows a customer to plan for retirement. Part 2: Generate a Retirement Planning Table: It's hard to decide how much you need to save for retirement. To help your customer visualize how long her nest egg will last, write a program that allows the user to generate a retirement planning table showing the number of months the savings will last for various combinations of starting account balance...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT