Pseudocode CS Thinking Example 4
Follow the full solution, then compare it with the other examples linked below.
Example 4
hardA colleague writes pseudocode that mixes Python syntax, uses unclear variable names, and skips steps. Identify three problems with: 'x = input(); for y in x: if y == z: cnt+=1; print(cnt)'. Rewrite it properly.
Solution
- 1 Step 1: Problems: (1) Uses Python syntax (input, print, +=) instead of pseudocode keywords. (2) Variable names x, y, z, cnt are unclear. (3) z is undefined โ where does it come from?
- 2 Step 2: Rewrite: searchTerm = INPUT('Enter search term'). text = INPUT('Enter text'). SET count = 0. FOR EACH character IN text: IF character == searchTerm THEN count = count + 1. OUTPUT count.
- 3 Step 3: Good pseudocode uses descriptive variable names, standard keywords, and includes all necessary steps.
Answer
Problems: Python syntax, unclear names, missing definition for z. Good pseudocode uses clear names, universal keywords, and complete logic.
Pseudocode quality matters because it is a communication tool. Poor pseudocode defeats its purpose โ it should be clearer than actual code, not more confusing.
About Pseudocode
An informal, human-readable description of an algorithm using structured language that resembles code but is not tied to any specific programming language. Pseudocode uses plain English mixed with programming constructs like IF, WHILE, and FOR to describe logic without worrying about syntax rules.
Learn more about Pseudocode โMore Pseudocode Examples
Example 1 easy
Write pseudocode for a program that asks the user for two numbers and outputs the larger one.
Example 2 mediumConvert the following Python code to pseudocode: total = 0; for i in range(1, 11): if i % 2 == 0: to
Example 3 mediumWrite pseudocode for a program that reads a list of 10 numbers and outputs how many are above averag