Skip to content
HackoryWEB SECURITY ACADEMYProgramming

Unit 04 · Datos de entrada simulados · Lesson 02

Convertir texto a entero con int()

int() convierte un texto con un número entero en un número.

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 alguien escribe cuántas unidades quiere, input() entrega texto: "3", no 3. Con un texto no se puede calcular el precio. Primero hay que convertirlo.

Understand each piece

  • Todo llega como texto

    input() siempre devuelve texto, aunque la persona escriba números. Por eso la prueba te va a dar valores como cantidad_texto = "3", con comillas.

  • int()

    Convierte un texto que tiene un número entero en un número. int("3") da 3. También ignora los espacios de los bordes: int(" 10 ") da 10.

  • Texto + número

    Si intentás "3" + 2, Python se detiene con TypeError: no puede sumar un texto con un número.

Example

cantidad_texto = "3"
cantidad = int(cantidad_texto)
print(cantidad * 20)

cantidad_texto guarda el texto "3". int lo convierte en el número 3 y lo guarda en cantidad. Ahora sí se puede multiplicar: se muestra 60.

Before moving on: what will happen?

¿Qué va a mostrar este programa?
texto = "2"
numero = int(texto)
print(texto + texto, numero + numero)

Your mission

Calculá el total según la cantidad

La prueba crea la variable cantidad_texto con lo que escribió la persona, como texto (por ejemplo "3"). Cada unidad cuesta 15. Convertí el texto en número, calculá el total y mostrá estas dos líneas. Con "3", la salida tiene que ser: Cantidad: 3 Total: 45

  1. Escribí cantidad = int(cantidad_texto).
  2. Calculá total = cantidad * 15.
  3. Mostrá print("Cantidad:", cantidad) y print("Total:", total).

If you get stuck

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

The key idea

Lo que escribe una persona llega como texto. int() lo convierte en un número entero para poder hacer cuentas.

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.