Managing Quantities with Variables

In the previous lesson, you learned how to use variables in Lua to store simple pieces of information, such as your travel destination. Now, let’s build on that knowledge and see how variables can help you manage and calculate quantities — an important skill for any programmer.

What You'll Learn

In this lesson, you will learn how to use variables to keep track of numbers and perform calculations. For example, imagine you are planning a world trip and want to track how many days you will spend in each country. You can use variables to store these numbers and then add them up to find the total trip length.

Here’s a quick example:

  • We create three variables to store the number of days spent in each country.
  • We add these variables together and store the result in a new variable called total_days.
  • We use print to display a message that includes the total number of days for the trip.
  • This demonstrates how variables can be used to store numbers, perform calculations, and show results in Lua programs.

Output:

Why This Matters

Being able to store and work with numbers is a core part of programming. Whether you are planning a trip, tracking scores in a game, or managing inventory, you will often need to keep track of quantities and perform calculations. By mastering these skills, you will be able to write programs that are not only more useful but also more fun and interactive.

Ready to put this into practice? Let’s move on and start working with quantities in Lua!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal