The following needs to be in python For this application, you will need to create a Python application that reads the sales for 12 months from a file and calculates the total yearly sales as well as the average monthly sales. In addition, this program should let the user edit the sales for any month. Jan 14317 Feb 3903 Mar 1073 Apr 3463 May 2429 Jun 4324 Jul 9762 Aug 15578 Sep 2437 Oct 6735
*The following needs to be in python
For this application, you will need to create a Python application that reads the sales for 12 months from a file and calculates the total yearly sales as well as the average monthly sales. In addition, this program should let the user edit the sales for any month.
Jan | 14317 |
Feb | 3903 |
Mar | 1073 |
Apr | 3463 |
May | 2429 |
Jun | 4324 |
Jul | 9762 |
Aug | 15578 |
Sep | 2437 |
Oct | 6735 |
Nov | 88 |
Dec | 2497 |
Step 1
- 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()
- use the print() to show the text: [FirstName] [LastName]'s Monthly Sales
- 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
yearly - View yearly summary
edit - Edit sales for a month
exit - Exit program
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
- Define a new read_sales() function
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)
-
-
- return sales
-
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("Not a valid command. Please try again.\n")
print("Bye!")
-
- 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
average = total / count
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.
Step by step
Solved in 4 steps with 3 images