Exercise: Arithmetic Again

Exercise 5 Very easy

Prerequisites for the exercise

  1. Python Control Flow
  2. All previous chapters

Objective

Extend the program created in the previous Rudimentary Arithmetic exercise, to enable recomputation of any operation on two given values.

Description

In the previous exercise, we constructed a program that could perform one of a given list of operations on two input integers. However, as you many know, it was only possible to do this once.

After the first computation, the program ended and likewise we couldn't continue on with other computations, apart from having to rerun the program.

Now, you need to make a couple of modifications to the previous program so that it could perform as many computations as the user wants to, and then stop when the user is done with the computations.

The initial inputs of this program are same as those for the previous exercise.

However, when the output for the first computation is made (which is again the same as in the previous exercise), you should then ask the user the following question, after leaving a blank line.

Restart? y for Yes, n for No.

If the user inputs 'y', continue repeating the steps of the program after a blank line. In contrast, if the user inputs 'n', halt the program — no more computations could be performed now.

Below shown is a detailed example.

x: 10
y: 20
Operation: a
x + y = 30

Restart? y for Yes, n for No.
y

x: 65
y: 15
Operation: s
x - y = 50

Restart? y for Yes, n for No.
n

Take note of the blank lines in the shell snippet here. You must make sure that your code also gives them.

A blank line following the input for 'Restart? y for Yes, n for No.' should only be given if the user enters 'y' i.e. a new computation is begun after leaving a blank line. But if the user enters 'n', then your program should end immediately, without leaving a blank line.

View Solution

New file

Inside the directory you created for this course on Python, create a new folder called Exercise-5-Arithmetic-Again and put the .py solution files for this exercise within it.

Solution

First of all, when it's stated that the program should be able to continue on with another such computation, we think of repetition.

What is being repeated over here?

Well, the whole segment of code from the previous exercise — asking to enter a value for x and y and a value for the operation to perform on them, and then outputting the result of the operation — is being repeated.

And when code repetition comes i.e. something has to be executed again and again, we know right away that we need a loop.

But which one to use? Shall we use for or while?

Recall that for is useful to iterate over sequences, whereas while is more oriented for iteration until some condition becomes False.

In this case, it's clear that we need while.

Now the condition we'd go with in while's header is True. That is, the loop will never hault since the condition True would never become false.

But then how would we stop the loop? That's using break.

First off, we start with the code from the previous exercise, as is, but this time inside the loop, since it ought to be repeated.

while True:
    x = int(input('x: '))
    y = int(input('y: '))
    op = input('Operation: ')

    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)
    else:
        print('x % y =', x % y)

When this code completes (inside the loop), we then print a blank line, followed by asking the user whether they would like to perform another operation or just stop here. This is much like a confirmation prompt, whereby the user has to confirm a given action.

while True:
    x = int(input('x: '))
    y = int(input('y: '))
    op = input('Operation: ')

    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)
    else:
        print('x % y =', x % y)
    
print() # blank line
again = input('Restart? y for Yes, n for No.\n')

Notice the newline character (\n) here. It's given to receive input from the user starting from a newline. By default, as we know, input begins right after the string passed to input(). Hence, if we pass \n at the end of the string, the input cursor would appear on a new line.

The value of the confirmation prompt (in line 20) is stored in the variable again.

If again is 'n' (for 'No'), we break the loop, without executing any further. Otherwise, we continue on with the next iteration after printing a blank line.

This is accomplished below:

while True:
    x = int(input('x: '))
    y = int(input('y: '))
    op = input('Operation: ')

    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)
    else:
        print('x % y =', x % y)
    
    print() # blank line
    again = input('Restart? y for Yes, n for No.\n')

if again == 'n':
break
print() # another blank line

If the conditional in line 22 is fulfilled (and therefore break is executed), the print() statement following it is not read. This is because break ends execution and puts it out of the loop as soon as it's executed.

So, if again is 'n', a newline is not printed, which is just what the exercise asked us to do.

This completes our exercise!