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
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:
First we introduce the problem by using the print() function containing a string to print it out to the screen
Now we declare a new variable sweets to store the user input that we will get using the input() function. We use a string to hold our question and put that in the input() function.
Next we define a function to perform the calculation, sweet_problem() takes one argument to pass in the value of the user input when we call the function later on.
Inside the function we need to remember that the value that will be passed into the function will be a string - from the input() function. To change it to an int, so we can perform calculations on it, we will cast it by passing it to the int() function.
We will declare a new variable to hold the output from the int() function - which will now be an int!*
Now we can perform the calculation on the user input, we will include the calculation within the print() function. We have also included strings either side to make the answer more readable - separated by commas.
We also include a return value.
Finally, we call the sweet_problem() function passing in the user input at an argument.
* 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.