Advertisements
Advertisements
प्रश्न
|
Raj is the manager of a medical store. To keep track of sales records, he has created a CSV file named Sales.csv, which stores the details of each sale. The columns of the CSV file are: Product_ID, Product_Name, Quantity_Sold and Price_Per_Unit. |
Help him to efficiently maintain the data by creating the following user-defined functions:
- Accept() – to accept a sales record from the user and add it to the file Sales.csv.
- CalculateTotalSales() – to calculate and return the total sales based on the Quantity_Sold and Price_Per_Unit.
कोड लेखन
Advertisements
उत्तर
I.
import csv
def Accept():
product_id = input("Enter Product ID: ")
product_name = input("Enter Product Name: ")
quantity_sold = int(input("Enter Quantity Sold: "))
price_per_unit = float(input("Enter Price Per Unit: "))
with open('Sales.csv', 'a', newline='') as file:
writer = csv.writer(file)
writer.writerow([product_id, product_name, quantity_sold, price_per_unit])
print("Sales record added successfully.")
II.
def CalculateTotalSales():
total_sales = 0.0
with open('Sales.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
total_sales += int(row[2]) * float(row[3])
print("Total Sales is:", total_sales)shaalaa.com
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
