count = 0
for i in range(10):
x = int(input("Enter a whole number: "))
if x > 0:
count = count + 1
print(f"The count is {count}")
This code can be represented as pseudocode. This is useful because non-Python programmers (eg Java or PHP programmers) can follow the algorithm and code it in their language:
count ← 0
FOR i ← 1 TO 10
INPUT x
IF x > 0 THEN
count ← count + 1
END IF
END FOR
OUTPUT "The count is ",count
Another way to represent an algorithm is to design a flowchart. Here is the flowchart which represents the above algorithm.
word = input("Enter a 5 letter word: ")
while len(word)!=5:
word = input("Invalid! Try again!")
print("Continue...")
Take some paper and a pencil. How would you draw this algorithm as a flowchart?
So, we have looked at:
But what about post-condition loops? Here is an example in pseudocode:
REPEAT
INPUT word
UNTIL LENGTH(word) = 5
OUTPUT "Continue."
Oooh! Interesting! Is it the same as a WHILE loop?
Do some research! How can we represent this as a FLOWCHART? And... how can we code it in Python?!
pseudocodeWHILE ... DO ... END WHILE flowchartFOR... END FOR pre-condition loop IF... THEN... END IF count-controlled loop INPUT post-condition loopOUTPUT