FAQ: Variables - Declare a Variable

This community-built FAQ covers the “Declare a Variable” exercise from the lesson “Variables”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Swift

FAQs on the exercise Declare a Variable

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

@discobot I need you

Hi! To find out what I can do, say @discobot display help.

1 Like

@discobot display help

I currently know how to do the following things:

@discobot start {name-of-tutorial}

Starts an interactive tutorial. {name-of-tutorial} can be one of: New User, advanced tutorial.

@discobot roll 2d6

:game_die: 3, 6

@discobot quote

:left_speech_bubble: Loss is nothing else but change,and change is Natures delight. — Marcus Aurelius

@discobot fortune

:crystal_ball: You may rely on it

2 Likes

@discobot tell me my fortune

@discobot fortune

  1. burger

:crystal_ball: Without a doubt

@discobot fortune will i die

when do i know to use variable?
is variable the first thing I’m meant to do when starting to code

Variables are for storing data. It depends on the scope the variables need to be recognized in. It is good practice to declare variables at the top first thing, but you can always go back and add new variables later when you need them.

For example, you might declare variables “x” and “y”, and later want to add them together and store them in “sum”. Once you have x and y, then assign a value to them, you can go to the top and create a “sum” variable to use for this.

This is somewhat random, and not really a question…
…but fair warning to those who have not started this exercise:

actually put the current year! :grin:

I did this exercise in 2021, but put the year ‘2020’ (just for fun hehe) and the code outputted

Did you assign the correct year? It’s currently 2021 . You’re behind the times!

If you do a future year, the code will know, too…

Did you assign the correct year? It’s currently 2021 . You’re looking too far into the future!

Anyway, hope this post wasn’t too much of a spoiler, but something fun to know!

1 Like

@discobot hi this my 1 time

Hi! To find out what I can do, say @discobot display help.

@discobot display help

I currently know how to do the following things:

@discobot start {name-of-tutorial}

Starts an interactive tutorial just for you, in a personal message. {name-of-tutorial} can be one of: New User, advanced tutorial.

@discobot roll 2d6

:game_die: 3, 6

@discobot quote

:left_speech_bubble: The grand essentials of happiness are: something to do, something to love, and something to hope for. — Chalmers

@discobot fortune

:crystal_ball: You may rely on it

@discobot ok thiaks

hunter

@discobot 1 buger

hunter

@thebibliophile101 ello

hunter

How come you can’t just print the whole variable?