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
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](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F3c348cba-a63b-4ada-8013-6981e5c617ad%2Fda04fe01-84ee-4faf-ba45-02fe9a620e34%2Fgte82uy_processed.png&w=3840&q=75)
![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.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F3c348cba-a63b-4ada-8013-6981e5c617ad%2Fda04fe01-84ee-4faf-ba45-02fe9a620e34%2F86u1kl_processed.png&w=3840&q=75)

Trending now
This is a popular solution!
Step by step
Solved in 3 steps









