CBSE Class 12 Computer Science Question 81 of 136

Data File Handling — Question 20

Back to all questions
20
Question

Question 20

Write a program that reads characters from the keyboard one by one. All lower case characters get stored inside the file 'LOWER', all upper case characters get stored inside the file 'UPPER' and all other characters get stored inside file 'OTHERS'.

Answer
lower_file = open("LOWER.txt", 'w')
upper_file = open("UPPER.txt", 'w')
others_file = open("OTHERS.txt", 'w')
ans = 'y'
while ans == 'y':
    char = input("Enter a character: ")
    if char.islower():
        lower_file.write(char + "\n")
    elif char.isupper():
        upper_file.write(char + "\n")
    else:
        others_file.write(char + "\n")
    ans = input("Want to enter a character? (y/n): ")
lower_file.close()
upper_file.close()
others_file.close()
Output
Enter a character: e
Want to enter a character? (y/n): y
Enter a character: A
Want to enter a character? (y/n): y
Enter a character: D
Want to enter a character? (y/n): y
Enter a character: c
Want to enter a character? (y/n): y
Enter a character: 7
Want to enter a character? (y/n): y
Enter a character: @
Want to enter a character? (y/n): n

The file "LOWER.txt" includes:

e
c

The file "UPPER.txt" includes:

A
D

The file "OTHERS.txt" includes:

7
@