Convert pseudocode to Python
Codoritmo translates your algorithm, written in pseudocode with PSeInt's syntax, into a Python program you can run. It's the most direct way to see that what you learned with Escribir, Leer, Si and Mientras is already programming.
Opens the program in Codoritmo; choose Python in View › Translate To.
The same algorithm in pseudocode and in Python
The “Guess the Number” example and the Python that Codoritmo generates for it, untouched.
Pseudocode
Algoritmo AdivinaNumero Definir secreto, intento, intentos Como Entero Definir acerto Como Logico secreto <- Aleatorio(1, 100) intentos <- 0 acerto <- Falso Escribir "Pensé un número del 1 al 100. Tienes 7 intentos." Mientras (NO acerto) Y intentos < 7 Hacer intentos <- intentos + 1 Escribir "Intento ", intentos, ":" Leer intento Si intento = secreto Entonces acerto <- Verdadero SiNo Si intento < secreto Entonces Escribir Sin Saltar "Más alto" SiNo Escribir Sin Saltar "Más bajo" FinSi // Abs da la distancia sin importar el signo Si Abs(intento - secreto) <= 5 Entonces Escribir ", ¡estás muy cerca!" SiNo Escribir "..." FinSi FinSi FinMientras Si acerto Entonces Escribir "¡Correcto! Lo lograste en ", intentos, " intentos." SiNo Escribir "Se acabaron los intentos. Era el ", secreto, "." FinSiFinAlgoritmoPython generated by Codoritmo
# Este código fue generado por Codoritmo a partir del pseudocódigo.# Puede que no sea completamente correcto: revíselo antes de usarlo.import randomdef main(): secreto: int intento: int intentos: int acerto: bool secreto = random.randint(1, 100) intentos = 0 acerto = False print("Pensé un número del 1 al 100. Tienes 7 intentos.") while (not acerto) and intentos < 7: intentos = intentos + 1 print("Intento ", intentos, ":", sep="") intento = int(input()) if intento == secreto: acerto = True else: if intento < secreto: print("Más alto", end="") else: print("Más bajo", end="") if abs(intento - secreto) <= 5: print(", ¡estás muy cerca!") else: print("...") if acerto: print("¡Correcto! Lo lograste en ", intentos, " intentos.", sep="") else: print("Se acabaron los intentos. Era el ", secreto, ".", sep="")if __name__ == "__main__": main()This is exactly the code Codoritmo generates for this program, including the comment at the top, which is always in Spanish.
How to translate your pseudocode to Python
Write or open your algorithm
It must be free of syntax errors; the Problems panel lists any.
Choose Python
In View › Translate To, choose Python. The code tab in the side panel shows the translation.
Compare and copy
The translation updates as you type. Copy it, or download it as .py with File › Export As.
Run it
With Python 3 installed, run python3 file.py in a terminal.
Opens the program in Codoritmo; choose Python in View › Translate To.
What the translation takes care of
PSeInt arrays start at 1 and Python lists at 0, so datos[i] becomes datos[i - 1]. If your profile uses 0-based arrays, the index stays the same.
A Para loop with fixed bounds becomes for … in range(…). When the bounds depend on variables, it becomes a while loop that works whether it counts up or down.
Where a result has to match PSeInt, the code includes small helper functions: MOD uses math.fmod to keep PSeInt's sign, for example, and real numbers are printed rounded to 10 decimals.
PSeInt names are case-insensitive, so the code writes every name in lowercase to make each use refer to the same variable.
How each instruction is translated
Each row is one PSeInt instruction translated by Codoritmo inside a program with integers n and i, a real x, a text nombre and an array datos[5]. The output is generated when this page is built.
| Instruction | PSeInt | Python |
|---|---|---|
| Write (Escribir) | Escribir "Hola, ", nombre | print("Hola, ", nombre, sep="") |
| Write without a newline | Escribir Sin Saltar "Total: " | print("Total: ", end="") |
| Read an integer (Leer) | Leer n | n = int(input()) |
| Read a real number | Leer x | x = float(input()) |
| Random number | n <- Aleatorio(1, 6) | n = random.randint(1, 6) |
| If … else (Si … SiNo) | Si n MOD 2 = 0 Entonces
Escribir "par"
SiNo
Escribir "impar"
FinSi | if int(math.fmod(n, 2)) == 0:
print("par")
else:
print("impar") |
| Switch (Segun) | Segun n Hacer
1: Escribir "uno"
De Otro Modo: Escribir "otro"
FinSegun | if n == 1:
print("uno")
else:
print("otro") |
| While (Mientras) | Mientras n > 0 Hacer
n <- n - 1
FinMientras | while n > 0:
n = n - 1 |
| Repeat … until (Repetir) | Repetir
Leer n
Hasta Que n > 0 | while True:
n = int(input())
if n > 0:
break |
| For (Para) | Para i <- 1 Hasta 10 Hacer
Escribir i
FinPara | for i in range(1, 11):
print(i) |
| Array element | datos[1] <- n | datos[0] = n |
Codoritmo and PSeInt desktop
PSeInt desktop also exports to other languages. These are the differences.
What Codoritmo adds
- You see the translation next to the pseudocode, and it updates as you type.
- It also exports to C#, PHP, Pascal, Visual Basic, QBasic, MatLab and HTML, like PSeInt.
- The translation follows the chosen profile, such as 0-based or 1-based arrays.
What PSeInt desktop does that Codoritmo doesn't yet
- Its exporter has many years of classroom use; Codoritmo's is newer, so review the code before you hand it in.
Codoritmo is an independent project, inspired by PSeInt and compatible with its syntax and school profiles. It is not PSeInt and is not affiliated with it. Official PSeInt website
Frequently asked questions
Does the generated Python run as is?
For class programs, yes: it runs on Python 3 unchanged. It's still worth reviewing, as the comment at the top says. If an instruction can't be translated, Codoritmo says so in a comment in the code.
Which Python version do I need?
Python 3.6 or later. The code uses type annotations such as n: int, which Python 2 doesn't have.
Can I convert Python back to pseudocode?
No. The translation goes one way, from pseudocode to Python.
Examples to try
Solved programs, each with its code, its flowchart and a sample run.
Guess the Number
Guess a secret number from 1 to 100 in seven tries.
IntermediateTemperature Converter
Converts degrees Celsius to Fahrenheit and Kelvin.
BeginnerPalindromes
Checks whether a phrase reads the same forwards and backwards.
IntermediateBubble Sort
Sorts random numbers by swapping neighbors, and stops as soon as they're in order.
Intermediate
More Codoritmo tools
- PSeInt onlineWrite and run pseudocode with PSeInt's syntax from your browser.
- PSeInt alternativeAn honest comparison of Codoritmo and PSeInt desktop.
- Online flowchart makerYour algorithm's flowchart, generated from the code and animated as it runs.
- Trace tablesA table of every variable's value at each step, filled in while the program runs.
- Pseudocode to CYour algorithm translated into a C (or C++) program ready to compile.
- Pseudocode to JavaYour algorithm translated into a Java class with a main method.
- Block-based programmingBuild your algorithm with Scratch-like blocks and see the pseudocode they write.