CBSE · 083Class XII · 2024Section D4 marks
Question 32
2 April 2024 · Computer Science (083)
Sangeeta is a Python programmer working in a computer hardware company. She has to maintain the records of the peripheral devices. She created a csv file named Peripheral.csv, to store the details.
The structure of Peripheral.csv is:
text
[P_id, P_name, Price]where: - P_id is Peripheral device ID (integer) - P_name is Peripheral device name (String) - Price is Peripheral device price (integer)
Write user defined functions : - Add_Device() : to accept a record from the user and add it to a csv file, Peripheral.csv. - Count_Device() : to count and display number of peripheral devices whose price is less than 1000.
Answer
python
import csv
def Add_Device():
F = open("Peripheral.csv", "a", newline='')
W = csv.writer(F)
P_id = int(input("Enter the Peripheral ID: "))
P_name = input("Enter Peripheral Name: ")
Price = int(input("Enter Price: "))
W.writerow([P_id, P_name, Price])
F.close()
def Count_Device():
F = open("Peripheral.csv", "r")
L = list(csv.reader(F))
Count = 0
for D in L:
if int(D[2]) < 1000:
Count += 1
print(Count)
F.close()Explanation
Add_Device appends a new row in append mode. Count_Device reads all rows and counts those with Price < 1000.