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 be able to cast variables with user input.
Now we have seen how to cast the input from a user into an int if we need it, let's now look at some programs we can write using the guidelines we talked about in 4.1b.
We still want to write programs that will solve a problem, but now we are focusing on a 'user' of these programs and how to write a program that is readable as well as correct. Here we will have to write our programs carefully so that the user knows exactly what to to and how to present the results how the user would expect them.
Let's look at writing a simple program to work out the area and perimeter of a rectangle. We will focus on presenting the program and the answers in a readable way while responding to user input (and casting).
#Calculate area and perimeter of a rectangle #Explain what the program does to the user, and what to expect print("This program will calculate the area and perimeter of a rectangle.") print("The program will ask for the height and width of the rectangle in cm") #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 width = input("Enter the width of the rectangle in cm - do not write cm : ") #add a colon and space for clarity plus a warning not to write cm!* height = input("Enter the height of the rectangle in cm - do not write cm : ") #add a colon and space for clarity plus a warning not to write cm!* #Hide the algorithim from the user - we don't need to print this out for the user to see #cast the input for width (string) to an int new_width = int(width) #cast the input for height (string) to an int new_height = int(height) #Present the perimeter in a readable way print("The perimeter of the rectange is", (new_width + new_width + new_height + new_height), "cm.") #Present the area in a readable way print("The area of the rectange is", (new_width +new_height), "cm squared.")
* If the user entered '12cm' we could not turn this into an int as it contains digits and letters, hence the warning on the input.
We can now write programs that are now useful to the user, that is, someone else who has not written the program. They can: understand the program, know what inputs they can use and also understand the output of the program. We have learnt to write programs for other people - and that is the important thing! Users don't need to, or should have to know how the program works - our job as programmers is to make sure the program does what is says it does. If you remember when we looked at functions as boxes; a user should only know what they have to input to the box to get the correct answer, not have to worry about how it is worked out.
As you can see from the code we have written, it is becomming quite difficult to follow when things get complicated, this is where we should start to use functions to separate the calculations and make our code much easier to follow.