Unit 03 · Variables · Lesson 03
Reasignar una variable
Volver a asignar reemplaza el valor anterior.
- Difficulty: Fácil
- 10 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
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 asignarSi escribís stock = 30 y después stock = 25, la variable se queda con 25. El valor anterior se pierde.
stock = stock - 5Python 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?
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
- stock = 50 y print("Stock:", stock).
- stock = stock - 12 y el mismo print.
- 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.
- 1
Son seis líneas: tres que cambian stock y tres print.
- 2
Para restar 12 al valor actual se escribe stock = stock - 12.
- 3
Después de cada cambio, repetí print("Stock:", stock).
The key idea
Asignar de nuevo reemplaza el valor. En stock = stock - 12, Python calcula con el valor viejo y guarda el nuevo.
You passed this lesson
- Una variable puede cambiar a medida que avanza el programa.
- Hasta ahora guardaste números. En la próxima lección vas a guardar textos y unirlos.
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.