Course 2.1Lesson 4 of 4
In this project we write two programs that ask. A unit converter changes kilometres to miles and kilograms to pounds with fixed factors. A bill splitter divides a bill among friends with // and % to show what each pays and what is left over. We plan each program, write it in small steps, test it with numbers that we know the answers for, and then change it to make it our own.
Try the code and the simulations beside the lesson.
After this lesson you can
People travel, cook and shop with different units. A kilometre is not a mile, and a kilogram is not a pound. A converter does the arithmetic. The conversion factors are
0.621371 miles,2.20462 pounds.0.621371.Type 10 and 70 on two lines in the Input box, and run it. 10 kilometres is about 6.21 miles. 70 kilograms is about 154.3 pounds. Check one by hand: 10 times 0.62 is about 6.2. The program agrees.
0 and 0. Both answers must be 0.1 for the distance. The answer must be 0.62.42.195 (a marathon). The answer is about 26.22 miles.Keeping 0.621371 in one place means one change fixes every answer, if the number is ever wrong.
Friends share a bill. In India, we do not like to divide a rupee into pieces. So we want to know how much each pays in whole rupees, and what is left over.
//.%.Type 1000 and 3 in the Input box and run it. Each friend pays 333, and 1 rupee is left over. Check: 3 * 333 + 1 = 1000. The rule "quotient times divisor plus remainder" gives the total again, so the answer is right.
Choose one, and do it without looking at a solution.
10.0 km is 6.21 miles.each + left_over./ for a bill in whole rupees. 1000 / 3 is 333.3333333333333.float() for a distance such as 42.195. With int() it is a ValueError.factor = 0.621371, so that a change is made in one place.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:
// and % split an amount into whole parts and a remainder.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
0.621371 in a variable?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.