Practice Question 7
In this question, we will learn how to write a function that counts how many words in a text file start with a special symbol (like @, #, $, *). We will use “rejection logic” with the continue statement to solve this efficiently.
The Logic (How it works)
Before writing the code, let us first understand the logic behind the solution.
To solve this, we check the very first character of every word. Here is the teacher’s breakdown:
-
Open & Split: We open
words.txtand use.split()to get a list of all words. -
The
isalnum()Filter: For every word, we check the character at index 0 (word[0]). -
The
continueStatement: If the first character is an alphabet or a digit (isalnum()), we don’t want it. We usecontinueto skip it and move to the next word. -
The Counter: If the code reaches the
elseblock, it means the word starts with a symbol. We then increment ourcount.
Let’s Code as per the above logic :
def countSpecialSymbolWords():
f = open("words.txt", "r")
data = f.read()
wordlist = data.split()
count = 0
for word in wordlist:
# Check if the first character is a letter or number
if word[0].isalnum():
continue
else:
# If not alphanumeric, it's a special symbol!
count += 1
f.close()
return count
# To call the function and see the result:
print("Words starting with a special symbol:", countSpecialSymbolWords())
Important Notes for Students
As a teacher, I want you to pay attention to these logic details to ensure you get full marks:
-
Using
word[0]: This specifically targets the start of the word. In exams, students often forget to use the index and try to check the whole word, which leads to logical errors. -
Why use
continue? It makes your code look professional. It tells the computer: “If this is a normal letter or number, stop here and jump to the next word immediately.” -
The
elselogic: Since we skipped all letters and numbers, the only things left that can trigger theelseblock are special characters like!,@,#, etc.
Teacher’s Secret Tip for Exams:
If the examiner asks you to count words that start with a vowel or a digit, simply change the isalnum() condition to a custom check. The rest of the logic using continue will remain exactly the same.
If the examiner asks you to display the words instead of counting them, just replace the count += 1 line with print(word). The filtering logic will not change.
If the examiner asks you to find words that do NOT start with a special symbol, simply reverse the condition and count the words that satisfy word[0].isalnum().
one smart logic can solve many different exam questions, which is exactly what CBSE wants to test.
Want to practice more? If you found this helpful, I have compiled a full list of 30 practice problems just like this one. Each question focuses on a different logic to help you become a pro at File Handling.
Click here to view the full 30-Question Practice Set
Exam Special Recommendation: If you are studying in 12th class and preparing for your board exam, then go for the “Computer Science with Python Sample Paper Book“. It contains 3 previous years’ papers and 7 practice papers solved strictly as per the CBSE pattern. Click here to purchase your copy!