Course 1.2Lesson 5 of 5
In this project we write a shop bill in four steps: the facts, the cost of each kind of item, the bill before tax, the tax and the total, and the display. We keep each fact in a variable, so that one change updates the whole bill. We check the answer by hand, change the facts to test it, and read an error message when it breaks. The project uses everything in this course: variables, updates, types and good names.
Try the code and the simulations beside the lesson.
After this lesson you can
A shop sells pens and notebooks. A customer buys 3 pens at 12 rupees each and 2 notebooks at 45 rupees each. The shop adds a tax of 5 percent. We want a program that shows the bill.
Good programmers think first. Write the plan in plain words. Do the bill by hand for these numbers.
You now know the right answer: 132.3. When the program shows the same, it is probably right. This is the most important habit in this course: know the answer before you run the program.
Look at the plan. Which values do we have to keep? Each fact gets its own variable, with a name that says what it is.
pen_price, pen_count, notebook_price, notebook_count, tax_percent.pen_total, notebook_total, subtotal, tax, total.Why not write 36 and 90 in the program? Because the shop will change a price tomorrow. With variables, you change one line, and the whole bill is right. With numbers written into every line, you must find and change each one, and you will forget one.
Here is the whole program.
The program has four parts.
print() for each line. The spaces after the words make the colons line up, so the bill is easy to read.Each slider is one fact at the top of the program. Changing a fact never needs a change in the formulas below it.
A program that works for one set of numbers may still be wrong. Test it with new facts, and work out the answer by hand each time.
Change pen_count = 3 to pen_count = 10. By hand: 10 times 12 is 120, the subtotal is 120 plus 90, which is 210, the tax is 10.5, and the total is 220.5. Run it, and check.
Now change tax_percent = 5 to tax_percent = 0. The tax must be 0, and the total must be the same as the subtotal. Does it show that?
Programmers meet errors all the time. A good programmer reads the message instead of being afraid of it. Suppose you type pen_totl in one place instead of pen_total. What does the message say?
NameError: name 'pen_totl' is not defined tells you the name that Python cannot find. Look for it in the program, and compare it letter by letter with the variable that you made.Try it. Change pen_total to pen_totl in the line subtotal = pen_total + notebook_total. Run it, read the message, and fix it.
A project is yours only when you change it. Choose one of these, and do it without looking at a solution.
friends and show what each person pays, rounded to two decimal places with round().Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
tax_percent in a variable and not write 5 in the formula?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.