Create a program that allows the user to choose from a given range of arithmetic operations, and then performs that operation on two input numbers.
Back in the Addition Calculator exercise, we created a program to add two input numbers.
Now you need to extend that program and perform any given operation on the two input numbers, other than just addition. The desired operation is itself specified as an input value.
As before, we begin with asking the user to enter two numbers, and then the desired operation.
Shown below is the general form of the initial three input prompts of the program:
<input> denotes a number and
<operation> denotes a letter to specify the operation desired to be performed on
<operation> must be one of the following characters:
Finally, when all of the inputs are received, the program should output the following after leaving a blank line, if the given operation character was one of those mentioned above:
<y> are the input numbers
<operation_symbol> is the symbol to denote the corresponding operation in PHP, and
<result> is the result of the operation (as performed on
If the input operation character wasn't one of those mentioned above, then the program should simply output the text 'Unknown operation.', once again with a blank line before itself.
Shown below is an example:
10 + 20 = 30
Here's another example:
50 * 7 = 350
And yet another example:
Take note of the blank line after the
Operation: ... prompt in all these examples. Your code must produce this blank line.
Inside the directory you created for this course on Python, create a new folder called Exercise-4-Rudimentary-Calculator and put the .py solution files for this exercise within it.
We start off by laying out three different calls to
input() — first two for the integers
y; while the third for the arithmetic operation.
x = int(input('x: ')) y = int(input('y: ')) op = input('Operation: ')
Next up, we check which letter did the user enter in the last input prompt i.e. what is the value of
op. This is done using
x = int(input('x: ')) y = int(input('y: ')) op = input('Operation: ') # Blank line before output. print() if op == 'a': print(x, '+', y, '=', x + y) elif op == 's': print(x, '-', y, '=', x - y) elif op == 'm': print(x, '*', y, '=', x * y) elif op == 'd': print(x, '/', y, '=', x / y) elif op == 'e': print(x, '**', y, '=', x ** y) elif op == 'r': print(x, '%', y, '=', x % y) else: print('Unknown operation.');
With this, our exercise has its solution.
DRY — Don't Repeat Yourself
When programming, there is one principle to always keep in mind. The DRY principle.
DRY stands for Don't Repeat Yourself, and is fairly self explanatory. It says that you should not repeat yourself — or better to say, you should not repeat code.
In the last code snippet above, notice that we are merely repeating the
print() statement over and over again, with just changing the symbol for the operation and the actual expression to be computed.
Imagine if we had to change the output value made in any case. For example, let's suppose, we just had to print:
<result> denotes the result of performing the respective operation on
Shown below is an example:
The result: 350
To account for this change in the output value, we'd need to change each
print() statement in our original code.
Doesn't this sound quite inefficient? Well, it sure does. That's because we are repeating a lot in our code.
Following DRY, we must keep from repeating unwanted statements in our code, and rather replace them with statements that could ultimately enable us to make small changes (such as changing the output in the case above) in the code very easily.
So coming back to our code, we could do one very basic thing to make it much much more flexible. Let's see that:
x = int(input('x: ')) y = int(input('y: ')) op = input('Operation: ') is_op_known = True if op == 'a': result = x + y op_sym = '+' elif op == 's': result = x - y op_sym = '-' elif op == 'm': result = x * y op_sym = '*' elif op == 'd': result = x / y op_sym = '/' elif op == 'e': result = x ** y op_sym = '**' elif op == 'r': result = x % y op_sym = '%' else: is_op_known = False # Blank line before output. print() if is_op_known: print(x, op_sym, y, '=', result) else: print('Unknown operation.')
Instead of directly making an output in each conditional's body, we now create two variables:
result holding the result of the operation, and
op_sym holding the symbol for the operator.
At the end of the script, we make the desired output using
Time to clarify one thing...
Applying DRY doesn't always lead to shorter code. The principle just states to not repeat statements that shouldn't be repeated.
In our case, when we were repeating
print(), making a change to the output meant that we'd have to make a change to each
print() statement. Here,
print() was the thing that shouldn't be repeated.
But now, with only one
print() statement, if we need to make a change to the output, we just ought to make a change to this single
Without any doubts, this new code is way more flexible than the previous one.
Knowing how, and when to apply DRY is another paramount skill for programmers to have.
Yes, sometimes, we could move on with inflexible, repetitive pieces of code, as with the
print() statements in the solution above. This usually happens in personal projects, or in competitive programming, where we are just concerned with the output of the program.
But, when we work in large teams where multiple programmers collaborate on the same pieces of code, having repetitive stuff can not only make your work unpresentable, but also unprofessional.