Skip to content
HackoryWEB SECURITY ACADEMYProgramming

Unit 11 · Errores y excepciones · Lesson 01

Leer un mensaje de error

La última línea del error dice qué pasó.

Lesson content is in Spanish.

Python in your browser

The first time, Python downloads about 5 MB (only once; then it stays saved). If you can, use Wi-Fi.

Python is already saved in this browser: it will not download again.

Python downloads when you start reading.
  1. 01Understand
  2. 02Try
  3. 03Explain

The situation

Cuando Python no puede ejecutar una línea, se detiene y muestra un informe de error. Al principio asusta, pero es la mejor pista para arreglar el programa: dice qué pasó y en qué línea.

Understand each piece

  • Traceback

    Es el informe de error. Se lee de abajo hacia arriba: la última línea dice el tipo de error y una explicación.

  • Tipo de error

    TypeError (tipos que no se pueden combinar), ValueError (un valor que no sirve), NameError (un nombre que no existe), entre otros.

  • Línea del error

    Más arriba, el informe indica el número de línea donde ocurrió. Las líneas anteriores sí se ejecutaron.

Example

total = 45
# print("Total: $" + total) daría:
# TypeError: can only concatenate str (not "int") to str
print("Total: $" + str(total))

La línea comentada mezcla texto y número con +, y Python respondería con TypeError. La versión que se ejecuta convierte el número con str() y muestra Total: $45.

Before moving on: what will happen?

Este programa tiene un error en la segunda línea. ¿Qué muestra antes de detenerse?
print("Iniciando el sistema")
total = "150" + 30
print("Total:", total)

Your mission

Arreglá el TypeError

La prueba crea cantidad y precio (enteros). El programa del editor se detiene con TypeError. Leé la última línea del error, encontrá la línea que falla y arreglala para que muestre el total sin espacio después del $ y el agradecimiento. Con cantidad = 3 y precio = 15, la salida es: Total: $45 Gracias por tu compra

  1. Ejecutá y leé la última línea del error.
  2. El problema está en unir un texto con un número usando +.
  3. Convertí total con str() o usá una f-string.

If you get stuck

One hint at a time: the first points the way and the last almost solves it.

The key idea

El informe de error se lee de abajo hacia arriba: el tipo de error y su mensaje te dicen qué arreglar.

Solve with help

If you have already tried, you can see a reference solution. The lesson is marked as solved with help and adds no XP.