0% found this document useful (0 votes)
0 views2 pages

Coding Principles With Examples

This document outlines key coding principles with examples, including clarity and readability, the DRY principle, and the KISS principle. It emphasizes the importance of input validation, standard loop patterns, and algorithm thinking. Additionally, it provides guidance on using nested loops and sentinel values in coding practices.

Uploaded by

shauntmogale
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views2 pages

Coding Principles With Examples

This document outlines key coding principles with examples, including clarity and readability, the DRY principle, and the KISS principle. It emphasizes the importance of input validation, standard loop patterns, and algorithm thinking. Additionally, it provides guidance on using nested loops and sentinel values in coding practices.

Uploaded by

shauntmogale
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Coding Principles – With Examples

Use this as a quick exam guide with patterns and examples.

1. Clarity & Readability


Use meaningful names and clean formatting.
age = 20 # good
a = 20 # bad

2. DRY Principle
Avoid repeating code.

for i in range(2):
print('Hello')

3. KISS Principle
Keep logic simple.

if age >= 18:


print('Adult')

4. Input Validation
Ensure correct input.

value = int(input())
while value < 0:
value = int(input())

5. Standard While Loop Pattern (VERY IMPORTANT)


Used in most tests.

value = int(input())
while value != 0:
print(value)
value = int(input())

6. Running Total Example


Sum values until negative.

total = 0
value = float(input())
while value >= 0:
total += value
value = float(input())
print(total)

7. Sentinel Value
A value that stops the loop (e.g., 0 or negative).

8. Nested Loops (Diagram)


Outer loop = rows, Inner loop = columns.

for i in range(3):
for j in range(5):
print('*', end='')
print()

9. Algorithm Thinking
Steps: INPUT → PROCESS → LOOP → OUTPUT

10. Auto-Marker Rules


No prompts. Exact structure matters.

You might also like