Step 6 Recap and More
Table of contents
- What did we cover?
- Extra Task 6.1 Accept decimal numbers
- Extra Task 6.2 Show the result on one line
- Extra Task 6.3 Shorten the code
What did we cover?
You completed the first assignment: you taught the computer how to ask for inputs, convert these inputs from text into numbers, sum them and tell the user what the result is.
On the way, you used algorithms to write what you wanted to teach the computer in plain English. This step is essential to know what you want the computer to do.
Once you have formulated your algorithm, you can always ask Google for the elements you don’t know how to write in Python (or in another language). For instance, similar to the search of type
, you could have looked it up on Google: Python how to convert text into integer. It is why it is critical to always think in terms of an algorithm before writing code. It helps us figure out what we want the computer to do.
Didn’t you have enough from this assignment? Do you feel comfortable going further? There are a few steps you can take to improve this code.
Extra Task 6.1 Accept decimal numbers
So far, the program converts string
to integer
. What about a sum of decimal numbers? Write the algorithm for converting a decimal number, search on Google the Python syntax for such conversion and upgrade the code.
Extra Task 6.2 Show the result on one line
So far, the program shows the result on two lines as follows:
1
2
The answer is
8
Convert the variable sum
into a type string
and concatenate it with “The answer is “ so they appear on the same line.
Extra Task 6.3 Shorten the code
If you are comfortable with the code, you should be able to shorten it into three lines (excluding comments).
Warning: Don’t forget to update your comments as you transform your code, so that it remains consistent for other and your future self.