Advertisements
Advertisements
Question
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:
[P_id, P_name, Price]
whereP_id is
Peripheral device ID (integer)P_name is
Peripheral device name (String)Price is
Peripheral device price (integer)
Sangeeta wants to write the following 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.
Code Writing
Solution
import csv
def Add_Device():
csvfile=open("Peripheral.csv", "a")
cwriter=csv.writer(csvfile)
P_id=int(input("Input device id:"))
P_name=input("Input device name:")
P_Price=int(input("Input device price:"))
devdata=[P_id, P_name, P_Price]
cwriter.writerow(devdata)
csvfile.close()
def Count_ Device():
c=0
csvfile =open("Peripheral.csv" ," r")
creader = csv.reader(csvfile)
for rec in reader:
if int(rec[2]) < 1000:
c+=1
print("Devices with price less than 1000:",c)
csvfile.close()
shaalaa.com
Is there an error in this question or solution?