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.1b
LO: To be able to use the input() function.
Having looked at mathematical questions and then turning them into programs we have seen how we can solve problems using code. All of these programs have achieved their goal in solving these problems, but at this point they have solved a very specific problem. Although we have seen that by using variables they are easy to change depending upon the different data that we enter to them, the change has to be made by us; the programmer. We are now going to look at writing programs that will behave more like the programs we interact with on a day to day basis; they will ask the user to enter the data and then run an algorithim based upon that data and then, importantly, give the answer to the problem in a human readable way.
Let's have a look at some elements of our program that it will need to contain to be readable and usable by a human - keeping the user in mind when we write our programs from now on.
Introduce the program - tell the user what the program does.
Explain how to use the program - what inputs the program will accept.
Hide the algorithim from the user - the user does not need to know how the program is working, they do not need to see anything apart from the output (the answer they want).
Present the answer in a readable way - if the user is expecting the answer in cm then they should recieve the answer in cm.
We will look at a simple program that will ask for the users name and then say 'hello' to them. To do this we will use the input() function, this will allow us to ask a question or prompt the user for input by including the question or prompt as it's parameter - the function will accept a string as an argument for us to include the uestion/prompt. We will follow the guidelines we have just set out for our program:
#Greeting program #Introduce the program - tell the user what the program does print("This program will ask your name and then say hello to you.") #Explain how to use the program - use a question as a string argument #We will also need to assign the input to a variable to store (remember) what the user types in as a response to the question name = input("What is your name? ") #add a space for clarity #Present the answer in a readable way - we can separate the variable (name) with the rest of the response (a string) by using a comma print("Hello", name) #the comma will add a space automatically between the string and the variable
Let's look at this in detail so we understand what is going on:
print("This.. - we introduce the program by using the print() function to print out a string containing the instructions to the user.
Now we use a variable name to store (remember) what the user has entered through the input() function.
We pass in the string "What is your name?" as an argument in the input() function.
We give the output by combining a string "Hello" with the variable name (refering to the users input) using a comma to seperate the arguments in the parameters.
Experiment with the input() function and try adding additional strings to the output:
#Greeting program 2 #Introduce the program - tell the user what the program does print("This program will ask your name and then say hello to you.") #Explain how to use the program - use a question as a string argument #We will also need to assign the input to a variable to store (remember) what the user types in as a response to the question name = input("What is your name? ") #add a space for clarity #Present the answer in a readable way - we can separate the variable (name) with the rest of the responses (strings) by using a comma print("Hello", name, "have a nice day") #the comma will add a space automatically between the string and the variable
Remember to include a space at the end of your question/prompt when writing the argument (string) in the input() function otherwise IDLE will just write the user input right next to the question/prompt.