Course 2.1Lesson 3 of 4
A small program that asks has three parts: input, calculation and output. Plan them in words first. Ask with input() and change the answers to numbers, calculate with the formula, and show the answer in one clear line with print(). Use round() to keep the answer short. A good program tells the user what it asks for and what the answer means. Programs such as a temperature converter, a circle area and a tip calculator all follow the same plan.
Try the code and the simulations beside the lesson.
After this lesson you can
round(), andAlmost every small program has three parts, in this order.
Plan these three parts in words before you write any code. This habit saves time and prevents most mistakes.
Plan. Input: a temperature in Celsius. Calculation: the formula F = C * 9 / 5 + 32. Output: the temperature in Fahrenheit, rounded to one decimal place.
Type 37 in the Input box and run it. The answer is 98.6. This is the temperature of a healthy person, so the program is probably right. Now test it with a number that you know: 0 Celsius must give 32.0 Fahrenheit, and 100 Celsius must give 212.0. A program should be tested with numbers whose answers you know.
Every small program in this lesson has the same shape: an input, a calculation and an output. Only the formula in the middle changes.
Plan. Input: the radius. Calculation: area = pi * radius ** 2, with pi = 3.14159. Output: the area, rounded to two decimal places.
Type 5 in the Input box and run it. The area of a circle of radius 5 is about 78.54. Look at the line radius ** 2. The power comes before the multiplication, so the formula is right: pi * (radius squared).
Three friends eat in a restaurant. The bill is 800 rupees, and they want to leave a tip of 10 percent, and split the total.
Type 800, 10 and 4 on three lines of the Input box and run it. Look at the three parts. The input has three questions, and the Input box needs three lines. The calculation uses the variables from the input. The output shows three lines, each one with a label.
Check by hand: the tip is 10 percent of 800, which is 80. The total is 880. For 4 friends, each pays 220. The program shows the same.
A good program talks to the user.
"Radius in cm: " is better than "Number: ".print("Each:", each) is better than print(each).round(value, 2) for money, round(value, 1) for a temperature.cm, rupees, Celsius in the question and in the answer.bill = input("Bill: ") gives text, and bill * tip_percent / 100 fails.pi * radius ** 2 is right, and (pi * radius) ** 2 is wrong.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
37. The checker tells you if the answer is 98.6.int() or float() at the moment that you read them.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
round(value, 2) for money?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.