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.
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.
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
Add a few comments into the Python code to document what your program does.
Save the file and close the code editor.
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.
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.