-
Notifications
You must be signed in to change notification settings - Fork 0
Idea for a Course Long Project (A Simple Calculator)
This is really nice and traditional innit
Tell them you want to first build a calculator that does addition only Very simple: All they need to do is print the title: "Addition Calculator"
print("Addition Calculator")Tell them they need to get the two numbers to add together from the user too. Get them thinking about using input to do this
Show them that mathematical operations are easy to do in Python. Get them thinking about using the addition operator
Show that variables are a useful tool to store and manipulate values Get them to code the following:
print("Addition Calculator")
num_1 = input("Enter number 1: ")
num_2 = input("Enter number 2: ")
result = num_1 + num_2
print(result)This does not work properly. Tell them you'll take an aside to explain why.
Show that functions take in only certain types and return only certain types. Show them Python typecasting and where it goes wrong. Show them the concat operation too.
Get them to code the following (with some ✨aesthetic improvements✨):
print("Addition Calculator")
num_1 = int(input("Enter number 1: "))
num_2 = int(input("Enter number 2: "))
result = num_1 + num_2
print("The answer is " + str(result))Now, we wish to extend this to the four arithmetic operations: addition, subtraction, multiplication and division. The user not only needs to specify the two numbers but also what type of operation they want to carry out. But how do you do this? We will have to code four different lines for the four different operations, but we only want the relevant line to run!
#Python runs all the lines in order. The final value of result will always be num_1 divided by num_2, since it replaces the previous values in the last line
result = num_1 + num_2
result = num_1 - num_2
result = num_1 * num_2
result = num_1 / num_2This is where if statements come in.
The structure of an if statement goes:
if(condition):
#These lines will only execute if condition is TrueSo now we can write our calculator program:
print("All-Purpose Calculator")
num_1 = int(input("Enter number 1: "))
num_2 = int(input("Enter number 2: "))
operation = input("Enter operation (+,-,*,/): ")
if(operation == "+"):
result = num_1 + num_2
if(operation == "-"):
result = num_1 - num_2
if(operation == "*"):
result = num_1 * num_2
if(operation == "/"):
result = num_1 / num_2
print("The answer is " + str(result))This works! But we can simplify it with the else statement :
if(condition):
#These lines will only run if condition is True
else:
#These lines will only run if condition is FalseSince the only four options are +, -, * or /, we need not check in the last if statement if operation == "/" . We can just assume it is "/", since there are no other options.
print("All-Purpose Calculator")
num_1 = int(input("Enter number 1: "))
num_2 = int(input("Enter number 2: "))
operation = input("Enter operation (+,-,*,/): ")
if(operation == "+"):
result = num_1 + num_2
if(operation == "-"):
result = num_1 - num_2
if(operation == "*"):
result = num_1 * num_2
else:
result = num_1 / num_2
print("The answer is " + str(result))This works! But we can simplify it with the else statement :
if(condition):
#These lines will only run if condition is True
else:
#These lines will only run if condition is FalseAt this juncture, get them to test the four different operations. There should be an error when using + or -. Ask them if they know why it is not working.
Tell them the reason. How to fix? elif!
if(condition_1):
#These lines will only run if condition is True
elif(condition_2):
#These lines will only run if condition_1 is False and condition_2 is True
else:
#These lines will only run if condition is FalseWith elif,
print("All-Purpose Calculator")
num_1 = int(input("Enter number 1: "))
num_2 = int(input("Enter number 2: "))
operation = input("Enter operation (+,-,*,/): ")
if(operation == "+"):
result = num_1 + num_2
elif(operation == "-"):
result = num_1 - num_2
elif(operation == "*"):
result = num_1 * num_2
else:
result = num_1 / num_2
print("The answer is " + str(result))Tell them the aim is to add the square root operation to the calculator. First, square root only accepts one argument. So get them to use existing knowledge to add this consideration.
print("All-Purpose Calculator")
operation = input("Enter operation (+,-,*,/,sqrt): ")
if(operation == "sqrt"):
num_1 = int(input("Enter number: "))
#result = ...
else:
num_1 = int(input("Enter number 1: "))
num_2 = int(input("Enter number 2: "))
if(operation == "+"):
result = num_1 + num_2
elif(operation == "-"):
result = num_1 - num_2
elif(operation == "*"):
result = num_1 * num_2
else:
result = num_1 / num_2
print("The answer is " + str(result))We will not use math.sqrt!! That function is for soulless plebs like fifth year CS students. We will go the old fashioned way: the Babylonian method.
For the unfamiliar, to approximate the sqrt of n,
- Pick a random positive number a from 0 to n exclusive. (I like n / 2)
- Let b = n / a
- If abs(b-a) > EPSILON, let a be the arithmetic mean of a and b (geometric mean also can but like... ew) and repeat steps 2-3
- This converges surprisingly quickly
This is also a good time to introduce them to the concept of EPSILONs.
Using for loops, they should be able to code a rudimentary version which uses a fixed number of iterations.
print("All-Purpose Calculator")
operation = input("Enter operation (+,-,*,/,sqrt): ")
if(operation == "sqrt"):
num_1 = int(input("Enter number: "))
a = num_1 / 2
b = num_1 / a
for i in range(10):
a = (a + b) / 2
b = num_1 / a
print("The answer is " + str(a))
else:
num_1 = int(input("Enter number 1: "))
num_2 = int(input("Enter number 2: "))
if(operation == "+"):
result = num_1 + num_2
elif(operation == "-"):
result = num_1 - num_2
elif(operation == "*"):
result = num_1 * num_2
else:
result = num_1 / num_2
print("The answer is " + str(result))For the sake of ✨knowledge✨, we wanna show the process as it is happening. Show them that i is a variable in and of itself and get them to print the result at each iteration.
print("All-Purpose Calculator")
operation = input("Enter operation (+,-,*,/,sqrt): ")
if(operation == "sqrt"):
num_1 = int(input("Enter number: "))
a = num_1 / 2
b = num_1 / a
for i in range(10):
print("Iteration " + str(i) + ": a = " + str(a) + ", b = " + str(b))
a = (a + b) / 2
b = num_1 / a
print("The answer is " + str(a))
else:
num_1 = int(input("Enter number 1: "))
num_2 = int(input("Enter number 2: "))
if(operation == "+"):
result = num_1 + num_2
elif(operation == "-"):
result = num_1 - num_2
elif(operation == "*"):
result = num_1 * num_2
else:
result = num_1 / num_2
print("The answer is " + str(result))Gasp! Why does it start from 0?? Teach them zero-index.
print("All-Purpose Calculator")
operation = input("Enter operation (+,-,*,/,sqrt): ")
if(operation == "sqrt"):
num_1 = int(input("Enter number: "))
a = num_1 / 2
b = num_1 / a
for i in range(10):
print("Iteration " + str(i + 1) + ": a = " + str(a) + ", b = " + str(b))
a = (a + b) / 2
b = num_1 / a
print("The answer is " + str(a))
else:
num_1 = int(input("Enter number 1: "))
num_2 = int(input("Enter number 2: "))
if(operation == "+"):
result = num_1 + num_2
elif(operation == "-"):
result = num_1 - num_2
elif(operation == "*"):
result = num_1 * num_2
else:
result = num_1 / num_2
print("The answer is " + str(result))We have no guarantee that the 10 iterations will be enough. Now teach them while loops and EPSILONs. Point out that a seems to always be larger than b. So a - b > EPSILON is enough.
EPSILON = 0.05
print("All-Purpose Calculator")
operation = input("Enter operation (+,-,*,/,sqrt): ")
if(operation == "sqrt"):
num_1 = int(input("Enter number: "))
a = num_1 / 2
b = num_1 / a
while(a-b > EPSILON):
print("Iteration " + str(i + 1) + ": a = " + str(a) + ", b = " + str(b))
a = (a + b) / 2
b = num_1 / a
print("The answer is " + str(a))
else:
num_1 = int(input("Enter number 1: "))
num_2 = int(input("Enter number 2: "))
if(operation == "+"):
result = num_1 + num_2
elif(operation == "-"):
result = num_1 - num_2
elif(operation == "*"):
result = num_1 * num_2
else:
result = num_1 / num_2
print("The answer is " + str(result))Let's add some more operations! What about absolute? Teach them how to build functions
EPSILON = 0.05
def custom_abs(n):
if(n >= 0):
return n
else:
return -n
print("All-Purpose Calculator")
operation = input("Enter operation (+,-,*,/,sqrt,abs): ")
if(operation == "sqrt"):
num_1 = int(input("Enter number: "))
a = num_1 / 2
b = num_1 / a
while(a-b > EPSILON):
print("Iteration " + str(i + 1) + ": a = " + str(a) + ", b = " + str(b))
a = (a + b) / 2
b = num_1 / a
print("The answer is " + str(a))
elif(operation == "abs"):
num_1 = int(input("Enter number: "))
result = custom_abs(num_1)
print("The answer is " + str(result))
else:
num_1 = int(input("Enter number 1: "))
num_2 = int(input("Enter number 2: "))
if(operation == "+"):
result = num_1 + num_2
elif(operation == "-"):
result = num_1 - num_2
elif(operation == "*"):
result = num_1 * num_2
else:
result = num_1 / num_2
print("The answer is " + str(result))Show them the multiple uses of arrays!
-
Simplifying the switch cases
-
Calculator memory
-
Average function
EPSILON = 0.05
single_ops = ["sqrt","abs"]
def custom_abs(n):
if(n >= 0):
return n
else:
return -n
print("All-Purpose Calculator")
operation = input("Enter operation (+,-,*,/,sqrt,abs): ")
if(operation in single_ops):
if(operation == "sqrt"):
num_1 = int(input("Enter number: "))
a = num_1 / 2
b = num_1 / a
while(a-b > EPSILON):
print("Iteration " + str(i + 1) + ": a = " + str(a) + ", b = " + str(b))
a = (a + b) / 2
b = num_1 / a
print("The answer is " + str(a))
else:
num_1 = int(input("Enter number: "))
result = custom_abs(num_1)
print("The answer is " + str(result))
else:
num_1 = int(input("Enter number 1: "))
num_2 = int(input("Enter number 2: "))
if(operation == "+"):
result = num_1 + num_2
elif(operation == "-"):
result = num_1 - num_2
elif(operation == "*"):
result = num_1 * num_2
else:
result = num_1 / num_2
print("The answer is " + str(result))1+2.
EPSILON = 0.05
single_ops = ["sqrt","abs"]
history = []
def custom_abs(n):
if(n >= 0):
return n
else:
return -n
print("All-Purpose Calculator")
operation = input("Enter operation (+,-,*,/,sqrt,abs): ")
if(operation in single_ops):
num_1 = int(input("Enter number: "))
if(operation == "sqrt"):
a = num_1 / 2
b = num_1 / a
while(a-b > EPSILON):
print("Iteration " + str(i + 1) + ": a = " + str(a) + ", b = " + str(b))
a = (a + b) / 2
b = num_1 / a
print("The answer is " + str(a))
else:
result = custom_abs(num_1)
print("The answer is " + str(result))
elif('h' in operation):
if(len(operation) > 1):
index = int(operation[1:])
if(index <= len(history):
print("The answer is " + str(history[index-1]))
else:
print(history)
else:
num_1 = int(input("Enter number 1: "))
num_2 = int(input("Enter number 2: "))
if(operation == "+"):
result = num_1 + num_2
elif(operation == "-"):
result = num_1 - num_2
elif(operation == "*"):
result = num_1 * num_2
else:
result = num_1 / num_2
history.append(result)
print("The answer is " + str(result))1+2+3.
EPSILON = 0.05
single_ops = ["sqrt","abs"]
history = []
def custom_abs(n):
if(n >= 0):
return n
else:
return -n
print("All-Purpose Calculator")
operation = input("Enter operation (+,-,*,/,sqrt,abs): ")
result = ''
if(operation in single_ops):
num_1 = int(input("Enter number: "))
if(operation == "sqrt"):
a = num_1 / 2
b = num_1 / a
while(a-b > EPSILON):
print("Iteration " + str(i + 1) + ": a = " + str(a) + ", b = " + str(b))
a = (a + b) / 2
b = num_1 / a
print("The answer is " + str(a))
else:
result = custom_abs(num_1)
print("The answer is " + str(result))
elif(operation == 'average'):
numbers = []
key = ''
while (key != 'q'):
key = input("Type number (q to finish): ")
if(key != 'q'):
numbers.append(int(key))
result = sum(numbers) / len(numbers)
print("The answer is " + str(result))
elif('h' in operation):
if(len(operation) > 1):
index = int(operation[1:])
if(index <= len(history):
print("The answer is " + str(history[index-1]))
else:
print(history)
else:
num_1 = int(input("Enter number 1: "))
num_2 = int(input("Enter number 2: "))
if(operation == "+"):
result = num_1 + num_2
elif(operation == "-"):
result = num_1 - num_2
elif(operation == "*"):
result = num_1 * num_2
else:
result = num_1 / num_2
history.append(result)
print("The answer is " + str(result))Error handling:
Get them to see all the places where errors can occur.
- Operation may not be correct
- Entered value may not be number
- sqrt may not converge (eg negative numbers)
By a team of students from NUS High School, aiming to make coding easier for everyone.