Given the following code, I need help correcting it to follow these instructions using python (the csv file contains the following info...) Jan 14317 Feb 3903 Mar 1073 Apr 3463 May 2429 Jun 4324 Jul 9762 Aug 15578 Sep 2437 Oct 6735 Nov 88 Dec 2497

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Given the following code, I need help correcting it to follow these instructions using python

(the csv file contains the following info...)

Jan 14317
Feb 3903
Mar 1073
Apr 3463
May 2429
Jun 4324
Jul 9762
Aug 15578
Sep 2437
Oct 6735
Nov 88
Dec 2497

 

#import the csv module for read-write
import csv

#file name for csv
FILE_NAME = "monthly_sales.csv"

#Show title of editor
def display_title():
    print("FirstName LastName's Monthly Sales")
    #space
    print()

#show user menu display
def display_menu():
    print("Command Menu\n\n Monthly - View monthly sales\n yearly  - View yearly summary\n edit    - Edit sales for a month\n exit    - Exit program")


"""def read_sales():"""

#main function
def main():

    sales = []

    
    #write to the csv file    
    with open(FILE_NAME, "r", newline="") as file:
        reader = csv.reader(file)
        for row in reader:
            sales.append(row)

    #print out the display title and menu and then give user options to choose from
    while True:
        #show menu
        display_title()
        display_menu()
        command = input("Command: ")
        #begin of if loop
        if command == "monthly":
            view_monthly_sales(sales)
        #show yearly
        elif command == "yearly":
            view_yearly_summary(sales)
        #allow to edit
        elif command == "edit":
            edit(sales)
        #exit program
        elif command == "exit":
            break
        #give user error if invalid input is entered
        else:
            print("Not a valid command. Please try again.\n")
    #allow user to know program is ended
    print("Bye!")

    view_monthly_sales(sales)

    for row in sales:
        print(f"{row[0]} - {row[1]}")
    print()

    view_yearly_summary(sales)

    total = 0

    for row in sales:
        amount = int(row[1])
        total += amount

    # get count
    count = len(sales)
    
    # calculate average
    average = total / count
    average = round(average, 2)

    # format and display the result
    print("Yearly total:    ", total)
    print("Monthly average: ", average)        
    print()

#end of program
if __name__ == "__main__":
    main()

• Define a display_title() function and have it
• use the print() to show the text: [FirstName] [LastName]'s Monthly Sales
▪ Replace the placeholders with your own first and last name
• add a space using the print()
• Define a display_menu() function and have it
• Use the print() function several times to show the following text with a space
after:
COMMAND MENU
monthly - View monthly sales
View yearly summary
- Edit sales for a month
- Exit program
yearly
edit
exit
Step 2
• Define a main() function
• Call the display_title() function and the display_menu() function created in step 1.
• Test the program to see if the functions work as expected. You should see the
title and the command menu
• Create a new variable named sales and assign it the value of read_sales() function.
• Define a new read_sales() function
▪ Assign sales to an empty array
sales = []
▪ Open the csv file, read the file, and close the file.
with open(FILENAME, newline="") as file:
reader = csv.reader (file)
for row in reader:
sales.append(row)
Step 3
. Back in the main method
• Create a while loop that executes until the user types exit.
• Inside the loop, store the user's input in a variable called "command"
• Next, create an if, elif, elif,... for each of the command menu items
while True:
command=input("Command: ")
if command == "monthly":
view_monthly_sales (sales)
elif command == "yearly":
.
view_yearly_summary(sales)
elif command == "edit":
edit (sales)
elif command == "exit":
break
else:
print("Bye!")
print("Not a valid command. Please try again.\n")
• HINT: Notice the functions are not yet created. We will do this in the next few
steps.
• Add the following at the bottom of your code:
if __name__ == "_
"__main__": main()
Step 4
• Create a view_monthly_sales(sales) function that receives the sales as a parameter.
In the view_monthly_sales(sales) function, create a for loop that loops through all
items.
for row in sales:
print (f"{row[0]} - {row[1])")
• Add a space after using the print() function
Transcribed Image Text:• Define a display_title() function and have it • use the print() to show the text: [FirstName] [LastName]'s Monthly Sales ▪ Replace the placeholders with your own first and last name • add a space using the print() • Define a display_menu() function and have it • Use the print() function several times to show the following text with a space after: COMMAND MENU monthly - View monthly sales View yearly summary - Edit sales for a month - Exit program yearly edit exit Step 2 • Define a main() function • Call the display_title() function and the display_menu() function created in step 1. • Test the program to see if the functions work as expected. You should see the title and the command menu • Create a new variable named sales and assign it the value of read_sales() function. • Define a new read_sales() function ▪ Assign sales to an empty array sales = [] ▪ Open the csv file, read the file, and close the file. with open(FILENAME, newline="") as file: reader = csv.reader (file) for row in reader: sales.append(row) Step 3 . Back in the main method • Create a while loop that executes until the user types exit. • Inside the loop, store the user's input in a variable called "command" • Next, create an if, elif, elif,... for each of the command menu items while True: command=input("Command: ") if command == "monthly": view_monthly_sales (sales) elif command == "yearly": . view_yearly_summary(sales) elif command == "edit": edit (sales) elif command == "exit": break else: print("Bye!") print("Not a valid command. Please try again.\n") • HINT: Notice the functions are not yet created. We will do this in the next few steps. • Add the following at the bottom of your code: if __name__ == "_ "__main__": main() Step 4 • Create a view_monthly_sales(sales) function that receives the sales as a parameter. In the view_monthly_sales(sales) function, create a for loop that loops through all items. for row in sales: print (f"{row[0]} - {row[1])") • Add a space after using the print() function
Step 5
• Create a view_yearly_summary(sales) function that receives the sales as a parameter
• Create a total variable and set it equal to 0.
• Create a for loop that loops through each row in the sales.
• Create an amount variable and assign it to the integer value in row 2 (row[1]).
• Add to the total the amount received in that row (total += amount)
• Get the count and store it in a variable named "count" (count = len(sales)).
• Create an average variable and assign it to the total / count (average = total /
count)
• Round the average to two decimal places.
• Display the yearly total and the monthly average
• Add a space below the results.
for row in sales:
amount = int(row[1])
total
amount
# get count
count len(sales)
=
# calculate average
total / count
average
average = round(average, 2)
# format and display the result
print("Yearly total:
", total)
print("Monthly average: ", average)
print ()
Step 6
• Create an edit(sales) function that receives the sales as a parameter
.
• Create a new variable named "names" in the new edit function.
• Assign it a list of abbreviated months (names = ['Jan', 'Feb, 'Mar', ... ])
• Create another variable named "name" and assign it the user's input for the
month.
• Make sure it is not case sensitive (name = name.title())
• If the input DOES NOT exist in the names list, display "Invalid three-letter month."
• If it does exist, move to step 6.
Step 7
• Create a variable named "index" and assign it the value of index of the month
entered (index = names.index(name))
Create a variable in the if statement named "amount" and assign it the value of an
integer from the user's input (amount = int(input("Sales Amount: "))
Create a "month" variable and assign it an empty list (month = [])
• Append the name to the list as well as the amount
month.append(name)
month.append(str(amount))
• Add the month and amount to the sales list at the correct index (sales[index] =
month)
• Call the method write_sales(sales) that we will create in step 7.
• Show the text "Sales amount for [month [0]} was modified."
• Add a space using the print() function.
Step 8
• Create the write_sales(sales) function and have sales as a parameter.
• Similar to the read() function, we will need to open, write, and close the file
with open (FILENAME, "w", newline="") as file:
writer
csv.writer (file)
writer.writerows (sales)
Step 9
• Test the application thoroughly to ensure there are no syntax or logical errors.
• Add a line of pseudo-code for EACH line of source code you created.
• Save the file.
Transcribed Image Text:Step 5 • Create a view_yearly_summary(sales) function that receives the sales as a parameter • Create a total variable and set it equal to 0. • Create a for loop that loops through each row in the sales. • Create an amount variable and assign it to the integer value in row 2 (row[1]). • Add to the total the amount received in that row (total += amount) • Get the count and store it in a variable named "count" (count = len(sales)). • Create an average variable and assign it to the total / count (average = total / count) • Round the average to two decimal places. • Display the yearly total and the monthly average • Add a space below the results. for row in sales: amount = int(row[1]) total amount # get count count len(sales) = # calculate average total / count average average = round(average, 2) # format and display the result print("Yearly total: ", total) print("Monthly average: ", average) print () Step 6 • Create an edit(sales) function that receives the sales as a parameter . • Create a new variable named "names" in the new edit function. • Assign it a list of abbreviated months (names = ['Jan', 'Feb, 'Mar', ... ]) • Create another variable named "name" and assign it the user's input for the month. • Make sure it is not case sensitive (name = name.title()) • If the input DOES NOT exist in the names list, display "Invalid three-letter month." • If it does exist, move to step 6. Step 7 • Create a variable named "index" and assign it the value of index of the month entered (index = names.index(name)) Create a variable in the if statement named "amount" and assign it the value of an integer from the user's input (amount = int(input("Sales Amount: ")) Create a "month" variable and assign it an empty list (month = []) • Append the name to the list as well as the amount month.append(name) month.append(str(amount)) • Add the month and amount to the sales list at the correct index (sales[index] = month) • Call the method write_sales(sales) that we will create in step 7. • Show the text "Sales amount for [month [0]} was modified." • Add a space using the print() function. Step 8 • Create the write_sales(sales) function and have sales as a parameter. • Similar to the read() function, we will need to open, write, and close the file with open (FILENAME, "w", newline="") as file: writer csv.writer (file) writer.writerows (sales) Step 9 • Test the application thoroughly to ensure there are no syntax or logical errors. • Add a line of pseudo-code for EACH line of source code you created. • Save the file.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY