Skip to content
Question 21 of 22

Q.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] where P_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.

Telangana TsbieCBSE Class XII Board 2024Subjective· 4mImportance★★★★★
95% · 21/22 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Sangeeta needs two functions: Add_Device() to append user-entered peripheral records to a CSV file, and Count_Device() to read the file and count devices priced below 1000.

The task sits at the intersection of file handling and practical data management in Python. Sangeeta's company maintains an inventory of peripheral devices—keyboards, mice, printers, scanners—and she's chosen the CSV (Comma-Separated Values) format because it's lightweight, human-readable, and plays nicely with both Python and spreadsheet software. The file structure is straightforward: each row holds a peripheral ID, its name, and its price.

Writing Add_Device()

This function needs to do three things: accept input from the user, open the CSV file in append mode, and write the new record without disturbing existing data.

import csv

def Add_Device():
    P_id = int(input("Enter Peripheral ID: "))
    P_name = input("Enter Peripheral Name: ")
    Price = int(input("Enter Price: "))
    
    with open('Peripheral.csv', 'a', newline='') as file:
        writer = csv.writer(file)
        writer.writerow([P_id, P_name, Price])
    
    print("Record added successfully!")

The 'a' mode is crucial—it positions the file pointer at the end, so new records stack below old ones rather than overwriting them. The newline='' parameter prevents Python from inserting extra blank lines on Windows systems, a quirk of how different operating systems handle line endings. The csv.writer object then takes the list [P_id, P_name, Price] and formats it as a proper CSV row with commas in the right places.

Note

If Peripheral.csv doesn't exist yet, opening in append mode will create it automatically. The first time Sangeeta runs this, Python generates the file on the fly.

Writing Count_Device()

This function reads the entire file, examines each record's price, and tallies how many fall below the 1000-rupee threshold.

import csv

def Count_Device():
    count = 0
    
    with open('Peripheral.csv', 'r') as file:
        reader = csv.reader(file)
        for row in reader:
            if int(row[2]) < 1000:
                count += 1
    
    print(f"Number of devices with price less than 1000: {count}")

The csv.reader object treats each line as a list. Since price sits in the third column, row[2] fetches it (remember Python counts from zero). Converting it to an integer with int() allows numerical comparison. The loop walks through every row, the if statement filters, and the counter increments each time a device qualifies.

Important

Always convert row[2] to an integer before comparing. CSV files store everything as strings, so without int(), Python would perform a lexicographic (alphabetical) comparison—where "900" might incorrectly appear "greater than" "1000" because '9' comes after '1' in character order.

Putting it together

If Sangeeta wants a small menu-driven program to use both functions:

import csv …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.