design, write and debug programs that accomplish specific goals, including controlling or simulating physical systems; solve problems by decomposing them into smaller parts.

use logical reasoning to explain how some simple algorithms work and to detect and correct errors in algorithms and programs

4.5b

LO: To write interactive programs using input(), functions and casting (2)

We will continue to look at mathematical problems but will now include the input() function to make these problems more universal - a program that will solve any simular problems is better than one than only solves one particular problem.


We will look at problems that involve more than one variable - we will have to ask the user for more than one input, but we will continue to structure these programs so that they fullfil the requirements we set out in 4.1b so that they are useful programs that can be used by a user that has no knowledge of how the program works - hiding the complexity from the user who only wants to get to the solution.


The best example to demonstrate our new skills is to look back on a multiplication table program we wrote back in 3.4b where we wrote a program to work out the miltiplication tables of a particular number - at that time we were learning how variables can help us change a program quickly (only make a change in one place):

#Year group or class
#name
#date
#Multiplication Tables with variables

#declare a variable and assign* a number to it
number = 9

def tables():
    print(number * 1)    #multiply the variable
    print(number * 2)
    print(number * 3)
    print(number * 4)
    print(number * 5)
    print(number * 6)
    print(number * 7)
    print(number * 8)
    print(number * 9)
    print(number * 10)
    print(number * 11)
    print(number * 12)

tables()

			

Now, with what we know now, we can see how we can make this program so much better by using the input() function along with variables to create a new program that will ask the user what number they want to find out the 1-12 multiplication tables for. Before you look below, I'm sure that you will be able to work this out given what we now know about input, casting and variable...



First, lets look at changing the code hopefully how you would have:

#Year group or class
#name
#date
#Multiplication Tables with input

#ask the user what number they want to find out the multiplication tables for
#remember to declare a variable to store the user input
#here we add a colon and a space for clarity
number = input("Type in a number to see the 1-12 multiplication tables for it: ")

#we have changed the function to accept one argument
def tables(number):
    #because the input is a string we will need to cast it to make it useful
    user_number = int(number)

    print(user_number * 1)    #multiply the variable
    print(user_number * 2)
    print(user_number * 3)
    print(user_number * 4)
    print(user_number * 5)
    print(user_number * 6)
    print(user_number * 7)
    print(user_number * 8)
    print(user_number * 9)
    print(user_number * 10)
    print(user_number * 11)
    print(user_number * 12)

#when we call the function we remember to pass in the variable for the user input
tables(number)

			

Now we can see that by using the input() function we have changed a simple program into something powerful and useful.



Now we can apply our requirements for a user friendly program to the code:

#Year group or class
#name
#date
#Multiplication Tables with input

#introduce the program - say what it does
print("This program will calculate the multiplication tables 1-12 for a number")

#explain to the use what they have to do
number = input("Type in a number to see the 1-12 multiplication tables for it: ")

#we have changed the function to accept one argument
def tables(number):
    #because the input is a string we will need to cast it to make it useful
    user_number = int(number)

    print(user_number * 1)    #multiply the variable
    print(user_number * 2)
    print(user_number * 3)
    print(user_number * 4)
    print(user_number * 5)
    print(user_number * 6)
    print(user_number * 7)
    print(user_number * 8)
    print(user_number * 9)
    print(user_number * 10)
    print(user_number * 11)
    print(user_number * 12)

#when we call the function we remember to pass in the variable for the user input
tables(number)

			

We could also add strings to the print() function to make the output even more readable - small things like this will make a huge difference to how 'user friendly' your program is:

#Year group or class
#name
#date
#Multiplication Tables with input

#introduce the program - say what it does
print("This program will calculate the multiplication tables 1-12 for a number")

#explain to the use what they have to do
number = input("Type in a number to see the 1-12 multiplication tables for it: ")

#we have changed the function to accept one argument
def tables(number):
    #because the input is a string we will need to cast it to make it useful
    user_number = int(number)

    print("1 x", user_number, "=", user_number * 1)    #multiply the variable
    print("2 x", user_number, "=", user_number * 2)
    print("3 x", user_number, "=", user_number * 3)
    print("4 x", user_number, "=", user_number * 4)
    print("5 x", user_number, "=", user_number * 5)
    print("6 x", user_number, "=", user_number * 6)
    print("7 x", user_number, "=", user_number * 7)
    print("8 x", user_number, "=", user_number * 8)
    print("9 x", user_number, "=", user_number * 9)
    print("10 x", user_number, "=", user_number * 10)
    print("11 x", user_number, "=", user_number * 11)
    print("12 x", user_number, "=", user_number * 12)

#when we call the function we remember to pass in the variable for the user input
tables(number)

			

We have done nothing new here, just entered strings separated by commas in the print() parameters, but you can see, by doing this, how much more readable the output is.



Hopefully by now, either you or the children have tried to enter something silly into the input() function; when it has asked for a number; someone has entered a word like 'bum', if you haven't, please do so. You will see that the program will crash - it can't cast a string 'bum' into an int - we will deal with these cases later on.

Code Examples

Resources