Vedansh is a Python programmer working in a school. For the Annual Sports Event, he has created a csv file named Result.csv, to store the results of students in different sports events. The structure of Result.csv is :

[St_Id, St_Name, Game_Name, Result]

Where

St_Id is Student ID (integer)

ST_name is Student Name (string)

Game_Name is name of game in which student is participating(string)

Result is result of the game whose value can be either 'Won', 'Lost' or 'Tie'

For efficiently maintaining data of the event, Vedansh wants to write the following user defined functions:

Accept() – to accept a record from the user and add it to the file Result.csv. The column headings should also be added on top of the csv file.

wonCount() – to count the number of students who have won any event.

As a Python expert, help him complete the task.

Answer

Answer by student

[Class 12] Vedansh is a Python programmer working in a school. For the - CBSE Class 12 Sample Paper for 2024 Boards

Detailed answer by teachoo

-lock-

To complete the task, Vedansh needs to write two user defined functions: accept() and wonCount() . Let us see how to write these functions step by step.

  • First, he needs to import the csv module which provides functions for reading and writing csv files in Python. He can do this by writing import csv at the top of his program.
  • Next, he needs to define the accept() function which will accept a record from the user and add it to the file Result.csv. He can do this by writing def accept(): followed by an indented block of code.
  • Inside the accept() function, he needs to do the following steps:
    • Open the Result.csv file in append mode using the open() function. This will allow him to add new records at the end of the file without overwriting the existing data. He can store the returned file object in a variable, say file , by writing file = open("Result.csv", "a") .
    • Create a csv writer object using the csv.writer() function and passing the file object as an argument. This will allow him to write data to the csv file in a formatted way. He can store the returned writer object in a variable, say writer , by writing writer = csv.writer(file) .
    • Write the column headings to the file using the writerow() method of the writer object and passing a list of strings as an argument. However, he should do this only if the file is empty, otherwise he will end up repeating the column headings. He can check if the file is empty by using the tell() method of the file object which returns the current position of the file pointer. If it is zero, it means that the file is empty. He can write an if statement to check this condition and write writer.writerow(["St_Id", "St_Name", "Game_Name", "Result"]) inside it.
    • Accept a record from the user as a list of values separated by commas using the input() function and splitting it by commas using the split() method . He can store the returned list in a variable, say record , by writing record = input("Enter St_Id, St_Name, Game_Name, Result separated by commas: ").split(",").
    • Write the record to the file using the writerow() method of the writer object and passing the record list as an argument. He can write writer.writerow(record) for this.
    • Close the file using the close() method of the file object. This will save any changes made to the file and free up any resources used by it. He can write file.close() for this.
  • After defining the accept() function, he needs to define another function called wonCount() which will count the number of students who have won any event. He can do this by writing def wonCount(): followed by an indented block of code.
  • Inside the wonCount() function, he needs to do the following steps:
    • Open the Result.csv file in read mode using the open() function. This will allow him to read the data from the file. He can store the returned file object in a variable, say file , by writing file = open("Result.csv", "r") .
    • Create a csv reader object using the csv.reader() function and passing the file object as an argument. This will allow him to read data from the csv file in a formatted way. He can store the returned reader object in a variable, say reader , by writing reader = csv.reader(file) .
    • Initialize a counter variable to store the number of students who have won any event. He can set it to zero initially by writing count = 0 .
    • Skip the first row of the file as it contains the column headings using the next() function and passing the reader object as an argument. He can write next(reader) for this.
    • Loop through each row in the file using a for loop and the reader object as an iterator. He can write for row in reader: followed by an indented block of code.
    • Inside the loop, check if the result column of the row is ‘Won’ using an if statement and indexing the row list by 3. He can write if row[3] == 'Won' : followed by an indented block of code.
    • Inside the if block, increment the counter variable by one using the += operator. He can write count += 1 for this.
    • After exiting the loop , close the file using the close() method of the file object. He can write file.close() for this.
    • Return the counter value using the return statement. He can write return count for this.
  • After defining both functions, he needs to call them and print their results. He can do this by writing:
    • accept() to add a record to the file.
    • print("Number of students who have won any event:", wonCount()) to print the number of students who have won any event.

The complete code with comments is given below:

part 2 - Question 35 - CBSE Class 12 Sample Paper for 2024 Boards - Solutions to CBSE Sample Paper - Computer Science Class 12 - Computer Science - Class 12

-endlock-

Remove Ads
Teachoo · Class 12 Explore Class 12
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh is an IIT Kanpur graduate and has been teaching for 16+ years. At Teachoo, he breaks down Maths, Science and Computer Science into simple steps so students understand concepts deeply and score with confidence.

Many students prefer Teachoo Black for a smooth, ad-free learning experience.