Unit 11 · Errores y excepciones · Lesson 01
Leer un mensaje de error
La última línea del error dice qué pasó.
- Difficulty: Media
- 20 XP
- Python
- Not started
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.
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
TracebackEs 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 errorTypeError (tipos que no se pueden combinar), ValueError (un valor que no sirve), NameError (un nombre que no existe), entre otros.
Línea del errorMá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?
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
- Ejecutá y leé la última línea del error.
- El problema está en unir un texto con un número usando +.
- 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.
- 1
La última línea del error dice TypeError: can only concatenate str (not "int") to str.
- 2
Significa que + recibió un texto y un número. total es un número.
- 3
La línea corregida es print("Total: $" + str(total)).
The key idea
El informe de error se lee de abajo hacia arriba: el tipo de error y su mensaje te dicen qué arreglar.
You passed this lesson
- Leíste un error, entendiste qué decía y lo corregiste.
- Algunos errores dependen de los datos, como un texto que no es un número. Esos se pueden atrapar para que el programa siga.
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.