Skip to content
HackoryWEB SECURITY ACADEMYProgramming

Unit 03 · Variables · Lesson 03

Reasignar una variable

Volver a asignar reemplaza el valor anterior.

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 stock de un producto cambia todo el tiempo: baja cuando se vende y sube cuando llega mercadería. Una variable puede seguir esos cambios.

Understand each piece

  • Volver a asignar

    Si escribís stock = 30 y después stock = 25, la variable se queda con 25. El valor anterior se pierde.

  • stock = stock - 5

    Python primero calcula la parte de la derecha con el valor que hay ahora y después guarda el resultado en la variable. Si stock valía 20, después vale 15.

Example

stock = 20
print(stock)
stock = stock - 5
print(stock)

Primero se guarda 20 y se muestra. Después Python calcula 20 - 5, guarda 15 en la misma variable y la muestra de nuevo.

Before moving on: what will happen?

¿Qué va a mostrar este programa?
cajas = 10
cajas = cajas + 4
cajas = cajas - 6
print(cajas)

Your mission

Seguí el stock de un producto

Creá la variable stock con el valor 50 y mostrala. Después restale 12 (se vendieron 12 unidades) y mostrala. Por último sumale 30 (llegó mercadería) y mostrala. Usá siempre print("Stock:", stock). La salida tiene que ser: Stock: 50 Stock: 38 Stock: 68

  1. stock = 50 y print("Stock:", stock).
  2. stock = stock - 12 y el mismo print.
  3. stock = stock + 30 y el mismo print.

If you get stuck

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

The key idea

Asignar de nuevo reemplaza el valor. En stock = stock - 12, Python calcula con el valor viejo y guarda el nuevo.

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.