3 - Variables

We are very close to having enough knowledge to actually start making some cool programs of our own. But before we get to that point we have to learn about variables.

Think back to our previous topic of inputs and outputs. We learned how to ask the user a question like “What is your favourite ice cream?”. But what happened to the value that the user entered as their answer? What if you wanted to print (output) the following message to the screen after the user has given their favourite flavour, “Cool! (Insert user’s flavour here) is a delicious flavour. My favourite flavour is chocolate.” 

input("What is your favourite ice cream flavour?")

print("Cool! _______ is a delicious flavour. My favourite

flavour is chocolate.")

You may have realized that we need to have a way to save the value that the user entered so that we can use it later. We do this by assigning the value to a variable. We use variables to store information so that we can manipulate or use it later. We want to name our variable something that describes what it is that we are storing. It is also important that your variable name has no spaces. So looking at our previous ice cream example we might store the user’s input as a variable as shown:

favoriteFlavour = input("What is your favourite ice cream flavour?")

print(favoriteFlavour)

Now if you run the program above you should see that whatever the user inputs as their favourite flavour is printed onto the screen. Try it a few times.

But back to original program above where we want to write the user’s favourite flavour in a message that includes our own favourite flavour of chocolate. We can actually join multiple phrases together inside of our print statement. We do this by just adding them together with the ‘+’ symbol.  It is important that each phrase has “quotation” marks at the start and end. We will discuss more about why this is important in the next section on data types.

favoriteFlavour = input("What is your favourite ice cream flavour?")

print("Cool!" + favouriteFlavour + "is a delicious flavour. My       favourite flavour is chocolate.")

If you run the code above you might notice one little problem. We are missing some spaces! Try updating your code to make it have the appropriate spaces. As a hint, you will need to add a space to the end of one phrase and the beginning of another.

3 - You try!

Write a program that asks the user 5 different questions. You can be creative with these questions. But for each question you need to write a reply that uses the user’s answer and also tells them what your answer to that question would be.

For example, you might ask
“What colour is your favourite?” and your reply might be “_______ is a very nice colour. I like blue.”