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.4b

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

We can now write programs that explain what the program does, interact with the user and provide readable results. Now we are going to look at using our skills at writing functions, solving mathematical problems and getting user input together!


We are going to look back at some of the written mathematical questions we have already solved but this time turning them into interactive programs that will solve all variations of them.


Let us start by looking at a problem we have already encountered; let's look at the sweets problem with John and Eric:

#John has 13 sweets.
#Eric has twice as many.
#How many sweets does Eric have?

#declare variables for the important numbers
sweets = 13

#here we need a function to take in one argument
def sweet_problem(num_of_sweets): #we will call the argument num_of_sweets
    print(num_of_sweets * 2) #we print out the answer
    return (num_of_sweets * 2) #we return the value of the input multiplied by 2 (notice the space!!!!)

#we call the function
sweet_problem(sweets) #the function expects one argument and we input the variable sweets

			

Let's use our new skills at using the input() function to expand this problem; we don't know how many sweets John has. It is important to see that a program that will work out the answer for an unknown number of sweets is far more useful than a program that just works out the answer for a given number. We have seen how the use of variables makes it quicker and easier for someone writing the code to change the values, but what we really want is a program to work out any value that the user comes across. Let's have another look at the program, but this time using the input() function to improve it...

#The sweet problem with input
#John has ? sweets. #John is the user here, he wants to find out how many sweets Eric has
#Eric has twice as many.
#How many sweets does Eric have?

#Introduce the problem
print("Eric has twice the number of sweets John has")

#call the input() function to find out how many sweets John has
sweets = input("How many sweets does John have? ") #space for clarity

#here we need a function to take in one argument
def sweet_problem(num_of_sweets): #we will call the argument num_of_sweets
    #remember we have to cast the string to an int!
    eric_sweets = int(num_of_sweets)
    print("Eric has", (eric_sweets * 2), "sweets" )#we print out the answer to be readable
    return (eric_sweets * 2) #we return the value of the input multiplied by 2 (notice the space!!!! - remember it is now an int!!!)

#we call the function
sweet_problem(sweets) #the function expects one argument and we input the variable sweets

			

Nothing here is new, but it worth looking in detail to see what is happening:



* This is a great example of including a return value in a function. To cast the string we pass it into the int() function and an int is returned for us to store as a variable. A value goes into a function, the complexity is hidden from us and then we get a value out.

Code Examples

Resources