7_3_2

WHAT: Work with variables

HOW:

Activity 1

Variables allow us to store data that is used in our programs. We can also change the data stored in those variables throughout the running of the program.

Try this code in Python:

name = ("Mr Maker")
print (name)

Run the code and see what happens.

Instead of printing the word "name" on the screen, it outputs Mr Maker. This is because we asked python to print the contents of a variable.

Activity 2

Create your own variable now and get it to print on the screen.

Activity 3

What happens when we use integers?

Try this code:

numA = (5)
numB = (10)
print (numA+numB)

Here we created 2 variables that stored numbers. We then found the sum of the 2 numbers within a print function.

Activity 4

Try this bit of code now, try to guess what will happen BEFORE you press F5 to run:

numA = (1)
numB = (numA+5)
print (numB)

Activity 5

Create your own bit of code now that displays the sum of two integers and a line of string. If you can't think of an idea then turn the algorithm below into Python code.

  • name = Derek
  • num1 = 1
  • num2 = 4
  • DISPLAY "My name is"
  • DISPLAY name
  • DISPLAY "I have two numbers, 1 + 4"
  • DISPLAY "When I add them together it makes..."
  • DISPLAY num1 + num2

Activity 6

We can ask the user of our program to give us some input to store in a variable. In Python, this looks like the code below. Try it out yourself!

name = input("What is your name? : ")
print ("Hello "+(name))

Ask another question and display it back to the user. e.g. what is your surname?

Activity 7

Using integers with input in Python is a little trickier. Try the code below and see what happens:

num1 = input("Type a number :")
num2 = input("Type another number :")
print (num1+num2)

Can you see what went wrong? What did it do?

Activity 8

Python automatically thinks that you are working with string when you use the input function. If you want it to store an integer then you MUST tell it.

Try this now...

num1 = int(input("Type a number :"))
num2 = int(input("Type another number :"))
print (num1+num2)

Did it work this time?

What has changed in the code?

Hopefully you spotted the int and the extra brackets that were added. This tells Python that you would like to store an integer.

Activity 9

With new skills come new problems. Try putting text as input when running the code in Activity 8 and see what happens!

This is a typical error message that you will see in Python. I usually look straight at the last line of code to see what went wrong.

It is telling me that we have a ValueError and that the data entered isn't an integer.

We will learn more about how to fix this kind of error later on but you can try the code below for now as a fix:

try:
    num1 = int(input("Type a number :"))
except ValueError:
    print ("You must enter a number")
    num1 = int(input("Type a number :"))
try:
    num2 = int(input("Type another number :"))
except ValueError:
    print ("You must enter a number")
    num2 = int(input("Type another number :"))

print (num1+num2)

We have added the words

try: 
except ValueError: 

This is saying, try to get some data entry except when there is a ValueError. If there is a ValueError then do this.

This short piece of code simply gives the user one more chance to get it right. We will learn how to loop this code later on to make it more robust!

CHECK:

EMBED:

Option One

Try working with variables in Scratch. It is quite different so you may need to use an online tutorial for a bit of help.

Option Two

Choose a mini program from the list below to make in Python:

  • The user types a number and it works out the times table for that number
  • The user enters a number and it uses a formula to turn the number into a secret pin
  • The user types their name and it gives them a silly response
  • Do a quick google search for madlibs images. Use text input from a user to create your own madlib.

CLASSROOM IDEAS:

Depending on the group, sometimes it helps to use cups with variable names on them with a short algorithm on the board.

e.g.

marbles = 0
add 1 to marbles
add 2 to marbles
subtract 1 from marbles

This kind of activity will help students to see that the variable name stays the same but we just change what is stored inside.