Skip to content
HackoryWEB SECURITY ACADEMYProgramming

Unit 04 · Datos de entrada simulados · Lesson 03

Convertir texto a decimal con float()

float() convierte un texto con decimales 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

El costo de un envío depende del peso del paquete, y la balanza marca valores como 0.5 o 1.75 kilos. int() no sirve para esos valores: hace falta otra conversión.

Understand each piece

  • float()

    Convierte un texto con decimales en número: float("1.5") da 1.5. También acepta enteros: float("2") da 2.0.

  • Punto, no coma

    Python escribe los decimales con punto. float("1.5") funciona, pero float("1,5") se detiene con un error.

  • Resultados con .0

    Una cuenta con un float da un float: 1.5 * 2 muestra 3.0, con el .0 al final.

Example

temperatura_texto = "21.5"
temperatura = float(temperatura_texto)
print(temperatura + 1)

float convierte "21.5" en el número 21.5. Sumarle 1 da 22.5.

Before moving on: what will happen?

¿Qué va a mostrar este programa?
peso = float("1.5")
print(peso * 2)

Your mission

Calculá el costo del envío

La prueba crea la variable peso_texto con los kilos del paquete, como texto con punto decimal (por ejemplo "0.5"). El envío cuesta 8 por kilo. Convertí el peso con float() y mostrá estas dos líneas. Con "0.5", la salida tiene que ser: Peso: 0.5 kg Costo de envío: 4.0

  1. Escribí peso = float(peso_texto).
  2. Mostrá el peso con print("Peso:", peso, "kg").
  3. Mostrá el costo con print("Costo de envío:", peso * 8).

If you get stuck

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

The key idea

float() convierte textos con decimales en números. Python usa punto decimal y muestra los float con su parte decimal.

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.