Paul's Python CoursePPC

Project 4 of 13, for Neil

Unit Converter

A menu that converts racing, flying and rocket speeds, so Neil can compare an F1 car with a jet.

About 50 minutes, in 15 steps, free and with no account needed.

Start the project

No account needed. Your work is kept in this browser until you sign up.

The brief

  • NeilF1 gives me speeds in km/h and American racing gives me mph, and I want to compare them.
  • NeilPlanes use knots, and rockets use Mach, which changes depending on how high you are.
  • NeilI'd love one converter for every speed I read about, though Mach's going to be the awkward one.
  • PaulThis time it'll have to choose what to do, and that's the new bit.

Print a menu of three conversions and ask which one (Choose 1, 2 or 3: ). Then ask for the speed (Speed? ), which may have a decimal point.

  • 1: km/h to mph. One mile is 1.609344 km, so divide by it.
  • 2: knots to km/h. One knot is 1.852 km/h, so multiply by it.
  • 3: Mach to km/h. Ask At sea level (yes or no)? . Mach 1 is 1225 km/h at sea level and 1062 km/h high up.
  • Anything else: print No such option.

Show the answer with two decimal places.

What it prints
1. Km/h to mph
2. Knots to km/h
3. Mach to km/h
Choose 1, 2 or 3: 3
Speed? 2
At sea level (yes or no)? no
That's 2124.00 km/h.

What you learn

The lesson, step by step

  1. Yes-or-no questions

    A comparison asks a question with a yes-or-no answer, like "is the speed over 300?". Python has six:

    • < less than, and > more than
    • <= less than or equal to, and >= more than or equal to
    • == equal to, and != not equal to

    The answer is True or False. With speed = 340, speed > 300 is True and speed == 300 is False.

    > means strictly more than, so 300 > 300 is False. >= is the one that lets 300 itself in.

    The usual mistake is = where you mean ==. One equals sign puts a name on a value. Two equals signs ask whether two values are the same.

  2. True and False

    True and False are values of their own type, called bool. They're written with a capital letter and no quotes, and every comparison gives back one of them.

    You can keep a bool in a variable, like any other value: is_fast = speed > 300. Then print(is_fast) prints True, and type(is_fast) is <class 'bool'>.

    The usual mistake is putting quotes round them. "True" in quotes is a string, four letters of text, not a bool. And lower-case true isn't anything: Python reads it as a name it doesn't know, and stops with a NameError.

  3. Faster than three hundred

    Neil hit exactly 300 km/h on the racing simulator. Two comparisons, one with > and one with >=.

  4. Choosing what to run

    if lets a program choose. It asks a question, and runs some lines only when the answer is True:

    if speed > 300:
        print("Faster than 300")
    

    The if line ends with a colon. The lines that belong to it, called its block, are indented four spaces (the Tab key puts them in). The block runs only when the test is True. else: adds a block for when it's False.

    The first line that isn't indented is outside the if, so it runs either way. On the right, Lap done always prints.

    The usual mistake is giving else a test of its own, like else speed <= 300:. else never has one. It takes whatever the if didn't.

  5. More than two ways

    elif, short for "else if", adds more questions between the if and the else. Python asks them from the top, runs the block of the first test that is True, and skips all the rest. Only one block ever runs, and else takes whatever is left.

    That makes a menu. Compare the choice with strings, if choice == "1":, because input() hands back a string, and a string never equals a number: "1" == 1 is False, even though they look alike.

    The usual mistake is expecting Python to pick the best match. It doesn't look ahead. It takes the first test that's True, so the order of the tests matters.

  6. First come, first served

    Neil's first try at sorting rocket flights put the tests in this order. Space starts 100 km up, and this flight reached 100.

  7. Which way it goes

    A two-option menu, with an else for anything else. The choices are in quotes because input() gives back a string.

  8. A decision inside a decision

    An if can go inside another if's block, indented one level further. This is called nesting. The inner question is only asked when the outer test was True. If the outer test is False, Python skips the whole block, inner if and all.

    That's how one option gets a question of its own. Neil only flies his model rocket in clear weather, and then only if the wind is under 20 km/h.

    The indent decides which else belongs to which if. An else pairs with the if it lines up with. The usual mistake is pairing them by distance, with the nearest if, instead of by indent.

  9. Launch or wait?

    The same launch rules, on a rainy day with almost no wind. Follow the indents to see which else belongs to which if.

  10. Rain first

    • NeilIf the track's wet it's wet tyres, and we only talk about the heat when it's dry.

    Neil picks tyres the way a race team does. A wet track gets wet tyres. A dry track gets softs, unless the track is 40 degrees or more, when softs wear out too fast.

    Put the lines in order and indent them, so the temperature is only checked when the track is dry. There are two else: lines: their indents decide which if each one belongs to.

  11. How Paul would do it

    • PaulThis is how I cost the class's server time, with a menu where one option needs a second question.

    A program like Neil's converter: Paul's menu for the cost of hiring server time. Option 2 needs a second question, so it has an if inside it. Run it with 2 and 4 hours: the first two hours cost 3.35, then 1.20 for each hour after, so 5.75.

    The else at the bottom catches anything that isn't on the menu: a 3, a typo, an empty answer.

    The four labels beside the code are Paul's steps. Your file for the Build starts with the same four, as comments.

  12. Fix Riya's version

    • RiyaI started Neil's converter with a menu, because nobody can remember which number is which.
    • RiyaBut every option says no such option. Can you have a look? Something's off.

    Riya's converter only does the first two options. Run it, choose 1 and type 300. It should say That's 186.41 mph.

    Work out why every choice ends up at No such option., then fix it.

  13. Make the unit converter

    Write Neil's converter. It shows a menu, asks for a choice and a speed, and prints the answer with two decimal places:

    • 1: km/h to mph. Divide by 1.609344.
    • 2: knots to km/h. Multiply by 1.852.
    • 3: Mach to km/h. Ask At sea level (yes or no)? . Mach 1 is 1225 km/h at sea level, and 1062 km/h high up, where the air is colder.
    • Anything else: No such option.
    1. Km/h to mph
    2. Knots to km/h
    3. Mach to km/h
    Choose 1, 2 or 3: 3
    Speed? 2
    At sea level (yes or no)? no
    That's 2124.00 km/h.
    

    Your file starts with Paul's four steps from the server-cost menu, as comments. Use if, elif and else for the menu, with the choices in quotes, and an if inside option 3 for its second question. The checks try every option, sea level and high up, and choices that aren't on the menu.

  14. Ship it

    • NeilThree hundred and forty km/h is two hundred and eleven mph? I always guessed lower than that.
    • NeilMach 2 up high is two thousand one hundred and twenty-four, and it worked that out before I'd finished reading.
    • PaulHe'll be converting every speed he can find over lunch now, you watch.

Guides that go with it