Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python help Invoice with txt.. I need to find a way to fo a to use function (getCopies()) that reads the file, line-by-line, and updates

Python help

Invoice with txt.. I need to find a way to fo a to use function (getCopies()) that reads the file, line-by-line, and updates the copies list you created. I don't even have a sample of how this works. Can you help?

Guidance:

Your script could have lists for the game names, number of copies, and the prices at the very top of your script (before function definitions). By doing it this way, all elements at index 0 will correspond to Fortnite, 1 to PUBG, and 2 to Candy Crush. With common lists, your script's main should first populate the number of copies for each game that was saved in your txt. If you do not create a global copies list, make sure your copies list is a parameter to your functions and is returned when updated.The functionality for this data loading should be in a function (getCopies()) that reads the file, line-by-line, and updates the copies list you created.

Your main should first load all of the saved data, by calling getCopies(), and only needs to load the data once per script run. Afterwards, your script should prompt the user for options. A single function can prompt the user for the number of copies and update the appropriate copies list element. When the user enters in a new copies amount, overwrite the txt file with the 3 game names and copy counts; Doing it this way, you do not need to worry about duplicates.

Lastly, the 4th menu choice should print the price and copies tallies by referring to the copies list you created and does not need to re-open the file.

Original Requirements:

Script name: invoice.py

A small video game retail store would like to automate their invoicing to keep track of product sales, how much they have made for each type of item, and the total amount of money made for all items. Create a script that performs a persistent invoice for products.

Prompt the user to select from the main menu below:

1. Fortnite ($29.99) 2. PUBG ($19.99) 3. Candy Crush ($4.99) 4. Print Invoice 5. Exit Your choice:

If the user enters 1-3, ask the user for the number of items sold for that item. For example, if the user selects 2, make the following prompt:

How many PUBG copies sold:

Your script should keep track of every item sold by adding the number of copies the user enters in the prompt above to the number of copies previously sold.

When the user enters in 4 from the main menu, the script should print the number of copies sold for each item, the amount of money made for each item's sales, and the amount of money made for all items together. These calculations should factor in data previously entered from past executions of your script. For example:

Fortnite: Copies sold: 3 Amount made: $89.97 PUBG: Copies sold: 2 Amount made: $39.98 Candy Crush: Copies sold: 0 Amount made: $0 Total: $129.95

The program should continuously prompt the user for input until the user selects 5 from the main menu to exit.

Requirements:

Save sales data to invoice_data.txt.

invoice_data.txt should not contain duplicated item data.

Handle the case where invoice_data.txt may not exist.

Use at least one list, one function, and one loop.

Do not add any additional prompts and output. Only follow what is required.

Comment your code.

my code from last try... of Script name: invoice.py .....................................

# The list is of format [forniteCopiesSold,pubgCopiesSold,candyCrushCopySold] copies=[0,0,0]

# This list is of Price [fornitePrice,pubgPrice,candyCrushPrice] price=[29.99,19.99,4.99]

# This list is of Titles titles=["Fortnite","PUBG","Candy Crush"]

# This function creates the invoice as a text file named invoice_data.txt def writeToFile(copies,price,total):

with open("invoice_data.txt","w") as file: # For Loop through the lists and print out each of them for i in range(3): file.write(titles[i]+":") file.write("\tCopies sold:"+str(copies[i])) file.write("\tAmount made: $"+str(copies[i]*price[i])) total=total+price[i]*copies[i] file.write("Total: $"+str(total)) # Close the file file.close()

# Using While True, since this will continue till the user breaks/quits # Will continously prompt for input

while True:

# List the choices for user print() print("Select your choice") print("1. Fortnite ($29.99)"); print("2. PUBG ($19.99)"); print("3. Candy Crush ($4.99)"); print("4. Print Invoice"); print("5. Exit");

# Stores the input of user based on option selected choice = int(input("Your choice: "));

print() # List what to do depending on choice if(choice==1): sold=int(input("How many copies were sold? ")) # will be updating the values for invoice copies[0]=copies[0]+sold

elif(choice==2): sold=int(input("How many copies were sold? ")) # will be updating the values for invoice copies[1]=copies[1]+sold

elif(choice==3): sold=int(input("How many copies were sold? ")) # will be updating the values for invoice copies[2]=copies[2]+sold

elif(choice==4):

# Prints the values collected for invoice and creates/writes it to a file print()

total=0

for i in range(3):

print(titles[i]+":") print("\t Copies sold: "+str(copies[i])) print("\t Amount made: $ "+str(copies[i]*price[i]))

total=total+price[i]*copies[i] # adds the totals

print("Total: $"+str(total)) writeToFile(copies,price,total)

elif(choice==5):

# Exit the program 5 is selected print("\t Thanks and goodbye!") break

else:

# User entered an invalid NUMBER apart from the ones stated

print("Please select valid number! The number you selected is not listed")

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

Structured Search For Big Data From Keywords To Key-objects

Authors: Mikhail Gilula

1st Edition

012804652X, 9780128046524

More Books

Students also viewed these Databases questions