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: Working with decimals/Using import.
Let's start with a problem. Back in 3.3b and 4.1a when variables were introduced you may have noticed that numbers with a decimal point (floats) were never mentioned. Try this simple calcuation in the shell of IDLE to see why:
#try 1.2 - 1.0 0.19999999999999996 #how about 1.3 - 1.0 0.30000000000000004
We're not going to go into details about what is happen here - it is how numbers with decimal places are represented using binary (0 and 1 used by computers). You can also think about how we represent numbers in our decimal system (0 to 9), this can lead to strange goings on:
if n / m = p then m * p = n
12 / 4 = 3 and 3 * 4 = 12
1 / 3 = 0.33 (or any number of decimal places)
0.33 * 3 ≠ 1
0.3333 * 3 ≠ 1
0.333333 * 3 ≠ 1
Let's just agree that it was a good idea not to have introduced this in year 3 or 4. We are going to look at a way of using decimals in Python that does not have this problem, to do this we are going to introduce the import statement
from module import a function
We have looked at functions like print(), range() and int(). These are known as built-in functions and are always available to us. By using the import statement we can get access to other specialised functions that are collected in modules. We are going to look at a module called decimal and import a function called Decimal()*
#write this at the beginning of your program from decimal import Decimal
Now we can try our simple subtraction questions again"
#write this at the beginning of your program from decimal import Decimal Decimal('1.2') - Decimal('1.0') Decimal(0.2) Decimal('1.3') - Decimal('1.0') Decimal(0.3)
Notice that we have written the numbers inside single quotes. When using variables we use Decimal() when we declare the variables:
#write this at the beginning of your program from decimal import Decimal #declare some float variables num1 = Decimal('1.3') num2 = Decimal('1.0') print(num1 - num2)
Would you be able to write a program that works out the area and perimeter of a square?
The program would have to explain what is does to the user.
The user should be able to enter the length of one side of the square in cm with one decimal point e.g. 12.3
Use seperate functions to calculate the area and perimeter and use the return statement.
The answer must be presented in a readable way for the user.
Have a look back at 4.3b to get you started.
To cast a string to a Decimal use Decimal(). You may want to pass around the string from the user_input to the functions and then convert to a Decimal:
#get user input user_input = input("Please enter...") #cast to a Decimal inside the function num1 = Decimal(number)
You can only perform calculations on a Decimal using another Decimal:
num1 = Decimal(number) num2 = Decimal('4.0') return num1 * num2
You can use the 6_game_loop_template.py found in the code examples to get you started.
* "There is a capital letter!"
Yes there is, this is a Decimal data type and not a function.