Test Index

CBSE Class 12 Computer Science 2025 Paper Compartment Get it on Google PlayDownload on the App Store

© examsnet.com
Question : 33 of 37
Marks: +1, -0
Suman is an intern at a software startup. The company has assigned her a task to create a CSV file named CLUB.CSV, to store the records of the Club members. After discussing with Club Incharge, Suman has planned to store the following content of members in the file CLUB.CSV :
[Mno, Name, Mobile, Fee]
Where
Mno – Member Number
Name – Name of the Member
Mobile – Member’s Mobile Number
Fee – Fee amount
Assuming you are asked to help Suman in her assignment, write a Python code for performing the following tasks with the help of user-defined functions :
NewMembers() : to accept records of members from the user and add them to the file CLUB.CSV.
PriorityMember() : to find and display those members from the file CLUB.CSV, who are paying Fee more than 35000.
Solution:  
Concept: The csv module writes a list as one comma-separated row with writer().writerow() and reads every row back as a list of strings with reader(). So the Fee (index 3) must be converted to a number before comparing it with 35000.
import csv
def NewMembers():
f = open("CLUB.CSV", "a", newline="") # append mode keeps old records
w = csv.writer(f)
n = int(input("How many members? "))
for i in range(n):
mno = int(input("Member Number: "))
name = input("Name: ")
mobile = input("Mobile Number: ")
fee = float(input("Fee: "))
w.writerow([mno, name, mobile, fee]) # one row per member
f.close()
def PriorityMember():
f = open("CLUB.CSV", "r", newline="")
r = csv.reader(f)
for rec in r: # rec = [Mno, Name, Mobile, Fee] as strings
if float(rec[3]) > 35000:
print(rec)
f.close()
NewMembers()
PriorityMember()
Key point: Open the file in append mode "a" in NewMembers() so earlier members are not erased, and use newline="" to avoid blank lines between rows.
© examsnet.com
Advertisements
Go to Question: