Exercise - Build a simple calculator app

Now that you've experimented with reading input and creating variables, let's try a slightly more complicated Python program - a simple additive calculator.

  1. Let's create a new Python script file named calc.py. Close the editor if it's still open and type the following into the terminal window.

    code calc.py
    
  2. In the script file, write the following program.

    • Prompt to enter the first number and assign it to a variable named x.
    • Prompt to enter a second number and assign it to a variable named y.
    • Add the two numbers together and assign the result to variable named result.
    • Display the two numbers and the result using print. The output should look something like: 2 + 2 = 4 (assuming the two numbers you input were both 2).
  3. Add a few comments into the Python code to document what your program does.

  4. Save the file and close the code editor.

  5. Run your program and verify that it runs as you expect. If you get an error, see if you can identify what the issue is and correct it.

  6. Here's some example output - note that your output doesn't have to match this exactly, but it should be similar.

Enter the first number: 10
Enter the second number: 20
10 + 20 = 30

If you run into trouble and can't get your app to run, you can move to the next unit to see an example solution of this exercise.