Convert algorithms 1 (Edexcel GCSE Computer Science): Revision Notes
Convert algorithms 1
Converting flowcharts into working Python programmes is a crucial skill in programming. Once you've designed an algorithm and drawn it as a flowchart, you need to translate it into actual code that a computer can run. This process involves understanding how different flowchart symbols correspond to specific Python instructions.
Understanding sequence
When converting algorithms, sequence refers to following steps in the correct order. In a flowchart, this order is shown by arrows connecting different shapes. When you write Python code, you achieve sequence by writing instructions one after another, with each line executing before the next one begins.
The computer reads your Python programme from top to bottom, just like following the arrows in a flowchart. This means the order you write your code matters - if you put instructions in the wrong sequence, your programme won't work correctly.
Converting input and output operations
Flowcharts use special parallelogram shapes to show when your programme needs to get information from the user or display results. These input and output symbols have direct equivalents in Python:
- Input symbols become input() functions in Python
- Output symbols become print() functions in Python
When converting these symbols, you need to think about what information is being requested or displayed. The flowchart might show "get name" which would translate to name = input("Enter your name") in Python.
Worked Example: Converting a Flowchart to Python
The flowchart shows a simple programme that collects a user's age and temperature, then adds them together. In Python, this becomes:
age = 0
total = 0.0
temperature = 18.0
age = int(input("Enter your age"))
temperature = float(input("Enter temperature"))
total = age + temperature
print("Total is:", total)
Notice how each flowchart symbol translates to specific Python code, maintaining the same logical sequence.
Handling start and stop symbols
The oval shapes at the beginning and end of flowcharts represent start and stop points. These are logical markers that help you understand where your algorithm begins and ends, but they don't translate to specific Python code.
In Python, your programme automatically starts when you run it and stops when it reaches the end of your code. You don't need special instructions for these flowchart symbols - they're just there to help you organise your thinking.
Working with different input and output devices
While input() and print() are the most common functions for getting and displaying information, Python can work with different devices. For example, when reading from files, you might use readline() instead of input(). Similarly, write() can be used instead of print() for file output.
These alternative functions might appear in more complex flowcharts, often shown within process symbols rather than standard input/output parallelograms.
Critical Data Type Conversion
One crucial aspect of converting algorithms is handling data types correctly. When you use input() in Python, it always gives you text (called a string), even if the user types numbers.
If your algorithm needs to perform mathematical calculations, you must convert these text inputs to numbers using:
- int() for whole numbers
- float() for decimal numbers
This conversion step is essential for any flowchart that involves arithmetic operations. Without it, Python will treat your numbers as text and won't be able to perform calculations correctly.
Practice and application
Converting algorithms requires practice with different types of flowcharts. Start with simple examples that involve basic input, processing, and output. As you become more confident, work with flowcharts that include more complex operations and multiple steps.
Remember that the goal is to maintain the logic and sequence of your original flowchart while using appropriate Python syntax and functions. Each flowchart symbol has a purpose, and understanding these purposes helps you write better code.
Key Points to Remember:
- Sequence matters - Python executes instructions from top to bottom, just like following flowchart arrows
- Use correct functions - Convert input symbols to input() and output symbols to print()
- Convert data types - Always use int() or float() when you need to do maths with user input
- Start and stop symbols don't need special Python code - they're just logical markers
- Practice regularly - The more flowcharts you convert, the more natural this process becomes