Exercise - Build a calculator

Completed

In this exercise, you'll learn to build a calculator. A calculator takes input and presents the result on the screen. After activating the sandbox, refresh the page to restart the Cloud Shell console.

  1. Start the REPL by typing python in the Cloud Shell console:

    python
    

    You should see output that looks similar to:

    Python 3.9.14 (main, Oct 29 2022, 22:18:10)
    [GCC 11.2.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    
  2. Copy and paste the following code into the REPL:

    first_number = int(input('Type the first number: ')) ;\
    second_number = int(input('Type the second number: ')) ;\
    print("The sum is: ", first_number + second_number)
    

    You should see the following output:

    >>> first_number = int(input('Type the first number: ')) ;\
    ...    second_number = int(input('Type the second number: ')) ;\
    ...    print("The sum is: ", first_number + second_number)
    Type the first number:
    

    The characters ;\, at the end of the first two statements signal that there are many lines of code. Using these characters lets you input all of the code, line by line. The code also runs, and as you can see, asks you for the first input.

  3. Complete the first input by typing 2 and pressing ENTER.

    You should see the following output:

    Type the first number: 2
    Type the second number:
    
  4. Now, complete the second input by typing 3 and pressing ENTER.

    You should see the following output:

    Type the second number: 3 
    The sum is:  5
    >>> 
    

Congratulations, you've created a calculator program.