KnowledgeBoat Logo
|

Computer Science

Shreya Sharma, a student of SMS School, has written a program to store Roman numbers and find their equivalents using a dictionary. She has written the following code. As a programmer, help her to successfully execute the given task.

import ............... #Line 1
numericals = {1: 'I', 4 : 'IV', 5: 'V', 9: 'IX',
            10: 'X', 40: 'XL', 50: 'L', 90: 'XC',
            100: 'C', 400: 'CD', 500: 'D', 900: 'CM', 
            1000: 'M'}
file1 = open("roman.dat", "...............") #Line 2
pickle.dump(numerals, file1)
file1.close()
file2 = open("roman.dat", " ............... ") #Line 3
num = pickle.load(file2)
file2. ............... #Line 4
n = 0
while n != -1:
    print("Enter 1,4,5,9,10,40,50,90,100,400,500,900,1000:")
    print("or enter -1 to exit")
    n = int(input("Enter numbers"))
    if n! = -1:
        print("Equivalent roman number of this numeral is : ", num [n])
    else:
        print("Thank You")

(i) Name the module she should import in Line 1.

(ii) In which mode Shreya should open the file to add data into the file in Line 2.

(iii) Fill in the blank in Line 3 to read the data from a binary file.

(iv) Fill in the blank in Line 4 to close the file.

Python File Handling

3 Likes

Answer

(i) Shreya should import the "pickle" module in Line 1 to work with binary file.

(ii) Shreya should open the file in write-binary mode ("wb") in Line 2 to add data to the file.

(iii) Shreya should fill in Line 3 with "rb" to read data from a binary file.

(iv) Shreya should use close() in Line 4 to close the file after reading the data.

Answered By

1 Like


Related Questions