Week 2: Variables, arithmetic, and reading errors¶
~9 min
This week's goal¶
This week you finish Part 1 of the Python course. You learn to store values in named variables, to use the arithmetic operators, and to read an error message from start to finish. By the end, you can write a small calculator and explain an error you caused yourself.
The main text is lessons 4 to 6 of Part 1. Look back at lesson 3 if input gives you trouble.
Day-by-day (about 1.5-2 hours per day)¶
Day 1. Read variables and types. Create a file types.py. Make one variable each for an integer, a decimal number, a string, and a boolean. Print each one with type(...). Write the results as comments next to the lines. Check that the types are what you expected before you run the file.
Day 2. Read arithmetic. The most useful operators are +, -, *, /, //, %, and **. The official tutorial shows that / always gives a float, that // discards the fractional part, and that % gives the remainder. Open the interactive prompt and try 17 / 3, 17 // 3, and 17 % 3. Write down what you see. Then try 2 ** 7.
Day 3. Write calculator.py. It reads two numbers with input, converts them with float(), and prints the sum, the difference, the product, and the quotient. Test it with whole numbers and with decimals. Notice that input gives you text, so the conversion is required. Keep the program short, under 20 lines.
Day 4. Read reading error messages. An error message has three parts you care about: the type of error, the message, and the line that caused it. Go back to the earlier file where you left a missing quote. Read the message again, and check that you can now find each of the three parts. Then add a line that divides by zero, for example print(1 / 0). Run it and read the ZeroDivisionError.
Day 5. Add a feature to the calculator. Ask the user for an operator (+, -, *, /) and print only that result. You will need if and elif, which are next week's topic. If that is too early, skip this step and do the next one. Add a comment at the top of the file that says what the program does.
Day 6. Write a short error log in your notes. For each of the three errors you have seen this week or last week, write the type, the line number, and one sentence on the cause. Then run calculator.py again and check that the logic still gives the right answers for 10 and 4: sum 14, difference 6, product 40, quotient 2.5.
Day 7 is a rest day.
Deliverable¶
calculator.py reads two numbers and prints four results. The notes file lists three errors with the error type, the line, and the cause. The notes file should show that you read each message and did not only copy the fix.
Check yourself¶
- What does
type(3.0)return, and why is it different fromtype(3)? - What is the value of
17 // 3, and what is the value of17 % 3? - Why does
input("Enter a number: ")followed byx + 1fail when the user types4? - What are the three parts of an error message that you should read first?
- A variable named
totalholds10. You then writetotal = total + 5. What doestotalhold now?
Answers
type(3.0)returnsfloat, because the number has a decimal point.type(3)returnsint.17 // 3is 5, and17 % 3is 2. Five times three plus two gives 17.inputreturns the string"4". Adding1to a string raises aTypeError. You need to convert the text first, withint()orfloat().- The type of error, the message that goes with it, and the line number where Python stopped.
- It holds 15. The right side is computed first, and the result is stored back in
total.