9_2_2

WHAT: Understand data types and use string and integers appropriately

HOW:

Activity 1 - READ

All data used needs to have a type. This makes it easier for an application to know what it should and can do with that piece of data.

There are five main types of data:

  • Integer (A whole number e.g. 5)
  • Real (A decimal number e.g. 5.5)
  • Character (A single letter e.g. "A")
  • String (A number of characters e.g "A dog went for a walk")
  • Boolean (True and False)

Activity 2

Copy and Paste this code into Python and see if it works:

a = input("Enter first number")
b = input ("Enter second number")
print (a+b)

What happens when the code is run? Enter two numbers to see what happens.

If you entered a 4 and then a 5 it would print 45. Why is this?

Activity 3

The default input setting in Python is String. It will think that anything entered via an input is a string unless told otherwise.

Change the code so that it matches the one below:

a = int(input("Enter first number"))
b = int(input ("Enter second number"))
print (a+b)

What has changed?

What happens now when this code is run?

Activity 4

You should now understand how to set a variable input as an integer, but what about the other data types? In Python, the commands to use for each data type are listed below:

  • String - str
  • Integer - int
  • Real - float
  • Boolean - bool
  • Character - str (with some extra validation - see below)

The simple code below shows how this might all look in Python:

a = int(input("Enter first number"))
b = int(input ("Enter second number"))
c = bool(input("True or False"))
d = float(input("Enter a number"))
e = str(input("Enter a character"))
lengthe=len(e) #checks the length of e <--Validation check
if lengthe>1: #if more than one character is entered
    print ("Data entered is not a character") #data validation check
    e = str(input("Enter a character")) # re-enter data
print (lengthe)
print (a)
print (b)
print (c)
print (d)
print (e)

The line of code where it checks if the length of e is our validation check.

Python doesn't have a data type for character so we must use validation checks to make sure that the right data has been entered.

CHECK:

EMBED:

In Activity 4 you were introduced to validation checks. You can create a validation check for all types of data entered. For character you would use a variation of the example shown. For specific data types you can use something called Try and Except. This is saying... Try to enter this data except when there is a value error.

try:
    a = int(input("Enter first number"))
except ValueError:
    print ("You must enter an integer")
    a = int(input("Enter first number"))

Try this piece of code to see how it works.

Challenge:

Create a questionnaire for a customer to fill in when starting a new Gym. The questionnaire must ask questions that would allow you to practice each data type check. Typical questions might be:

  • First initial (char)
  • Surname (string)
  • Street number (integer)
  • Street address (string)
  • Weight (real)
  • Have you been a member of a gym before? (Boolean)

CLASSROOM IDEAS:

There are some nice activities on error detection here:

http://csunplugged.org/error-detection/

This could help with the final embed task.