Test Index

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

© examsnet.com
Question : 36 of 37
Marks: +1, -0
A file CLINICAL_TRIALS.DAT stores records of the following structure :
[TRIAL_ID, PATIENT_NAME, DRUG_TESTED, DOSAGE_MG, OUTCOME_SCORE]
Where :
• TRIAL_ID - Unique identifier for the clinical trial participant (string type)
• PATIENT_NAME - Name of the patient (string type)
• DRUG_TESTED - Name of the drug administered (string type, e.g., "DrugX", "DrugY", "Placebo")
• DOSAGE_MG - Dosage of the drug in milligrams (integer type)
• OUTCOME_SCORE - Numerical score indicating the treatment outcome (float type, higher score means better outcome)
Write user-defined functions in Python for the following tasks :
(I) RecordTrialData() : This function should input data for clinical trial participants and write it to the binary file CLINICAL_TRIALS.DAT.
(II) AdjustLowDosage() : This function should increase the DOSAGE_MG of all participants whose OUTCOME_SCORE is below 0.5 by 20% and rewrite the updated records into the file CLINICAL_TRIALS.DAT.
Solution:  
Concept:
A binary file stores Python objects with the pickle module: pickle.dump(obj, file) writes one object and pickle.load(file) reads the next one. Each record here is a list of 5 items, so index 3 is DOSAGE_MG and index 4 is OUTCOME_SCORE. pickle.load() raises EOFError when the file ends, so records are read in a while True loop inside try ... except.
(I) Open the file in append-binary mode 'ab' (old records stay), input the five fields with the right types (int for dosage, float for score), form a list and dump it:
import pickle
def RecordTrialData():
f = open("CLINICAL_TRIALS.DAT", "ab")
n = int(input("How many participants? "))
for i in range(n):
trial_id = input("Trial ID: ")
name = input("Patient name: ")
drug = input("Drug tested: ")
dosage = int(input("Dosage (mg): "))
score = float(input("Outcome score: "))
rec = [trial_id, name, drug, dosage, score]
pickle.dump(rec, f)
f.close()
(II) Read every record into a list, raise the dosage by 20% where the score is below 0.5 (new dosage = old dosage ×1.2\times 1.2), then reopen the file in write-binary mode 'wb' and write all the records back:
import pickle
def AdjustLowDosage():
f = open("CLINICAL_TRIALS.DAT", "rb")
records = []
try:
while True:
rec = pickle.load(f)
if rec[4] < 0.5:
rec[3] = round(rec[3] * 1.2)
records.append(rec)
except EOFError:
f.close()
f = open("CLINICAL_TRIALS.DAT", "wb")
for rec in records:
pickle.dump(rec, f)
f.close()
print("Dosage updated for low outcome scores.")
Example run (two records entered with RecordTrialData()):
Records before:  ['T01', 'Asha', 'DrugX', 50, 0.4]
['T02', 'Ravi', 'Placebo', 40, 0.8]
After AdjustLowDosage():
['T01', 'Asha', 'DrugX', 60, 0.4]
['T02', 'Ravi', 'Placebo', 40, 0.8]
T01 has score 0.4<0.50.4 < 0.5, so its dosage becomes 50×1.2=6050 \times 1.2 = 60 mg; T02 (score 0.80.8) is unchanged.
Key point: DOSAGE_MG is an integer, so the new value is rounded with round() (writing rec[3] = rec[3] + rec[3] * 20 / 100 is also accepted; it stores a float such as 60.060.0). The file must be opened in 'wb' mode only after all records have been read, otherwise the data is lost.
© examsnet.com
Advertisements
Go to Question: