In a make task you use the skills you have learned to create a program from scratch. There is no starter code: you begin with an empty editor.
You are going to write a calculator. It takes two numbers and a choice of operation from the user, works the answer out in a sub program, and prints it.
Define four sub programs, named add, subtract, multiply and divide.
subtract takes the second number away from the first, and divide divides
the first by the second.Then write the main program, underneath the four definitions.
print, exactly as written:
Enter the first number:
Enter the second number:
Read each answer with a bare input() and cast both with int().
1 to add, 2 to subtract, 3 to multiply or 4 to
divide. Ask that question the same way, exactly as written:
Enter 1 to add, 2 to subtract, 3 to multiply or 4 to divide:
Read the answer with a bare input() and cast it with int().
if / elif / else chain on that choice to call the matching
sub program with the two numbers, and print the value it returns, converted to
a string, as part of a sentence.else at the end of the chain catches every choice that is not 1, 2, 3 or
The three questions are printed exactly as written above, followed by exactly one more line, whatever the user types.
When one of the four sub programs was called, that line is worded exactly like this:
The answer is 19
The words The answer is, then a space, then whatever the sub program handed
back. When the choice was not 1, 2, 3 or 4, the line is exactly this instead:
Sorry, that is not one of the four options
The / operator always produces a decimal number, even when the division comes
out exactly. 10 / 4 gives 2.5, and 8 / 2 gives 4.0 rather than 4.
That is what your program should print. Do not round the answer, and do not cast
it back to a whole number with int(): the tests expect what / actually gives
you. The other three operations work on whole numbers and print without a decimal
point.
Input:
12
7
1
Output:
Enter the first number:
Enter the second number:
Enter 1 to add, 2 to subtract, 3 to multiply or 4 to divide:
The answer is 19
Input:
40
55
2
Output:
Enter the first number:
Enter the second number:
Enter 1 to add, 2 to subtract, 3 to multiply or 4 to divide:
The answer is -15
(40 minus 55, so the answer is negative)
Input:
6
9
3
Output:
Enter the first number:
Enter the second number:
Enter 1 to add, 2 to subtract, 3 to multiply or 4 to divide:
The answer is 54
Input:
10
4
4
Output:
Enter the first number:
Enter the second number:
Enter 1 to add, 2 to subtract, 3 to multiply or 4 to divide:
The answer is 2.5
(10 divided by 4)
Input:
8
2
4
Output:
Enter the first number:
Enter the second number:
Enter 1 to add, 2 to subtract, 3 to multiply or 4 to divide:
The answer is 4.0
(the division comes out exactly, and / still gives a decimal number)
Input:
5
3
7
Output:
Enter the first number:
Enter the second number:
Enter 1 to add, 2 to subtract, 3 to multiply or 4 to divide:
Sorry, that is not one of the four options
(7 is not one of the four choices, so nothing is calculated)
All three questions are part of the output that is checked here, so their
spelling, capital letters and punctuation count. Ask each one with its own
print, exactly as written above, and read the answer with a bare input()
on the next line, rather than passing the question to input(...).
Make sure that you check for the following things:
def lines and at
the calls. Capital letters count.def, if, elif and else line.if chain in the main program.def line has two parameters, and each call passes two arguments.return, not print. A sub program that prints its
result hands nothing back, so there is nothing for the main program to store.input is wrapped in int(), including the one that collects the
choice, or the choice is compared as text and never matches.str() before it is joined onto the sentence with
+.