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.
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.
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
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
TrueorFalse. Withspeed = 340,speed > 300isTrueandspeed == 300isFalse.>means strictly more than, so300 > 300isFalse.>=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.True and False
TrueandFalseare 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. Thenprint(is_fast)printsTrue, andtype(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-casetrueisn't anything: Python reads it as a name it doesn't know, and stops with a NameError.Faster than three hundred
Neil hit exactly 300 km/h on the racing simulator. Two comparisons, one with
>and one with>=.Choosing what to run
iflets a program choose. It asks a question, and runs some lines only when the answer isTrue:if speed > 300: print("Faster than 300")The
ifline 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 isTrue.else:adds a block for when it'sFalse.The first line that isn't indented is outside the
if, so it runs either way. On the right,Lap donealways prints.The usual mistake is giving
elsea test of its own, likeelse speed <= 300:.elsenever has one. It takes whatever theifdidn't.More than two ways
elif, short for "else if", adds more questions between theifand theelse. Python asks them from the top, runs the block of the first test that isTrue, and skips all the rest. Only one block ever runs, andelsetakes whatever is left.That makes a menu. Compare the choice with strings,
if choice == "1":, becauseinput()hands back a string, and a string never equals a number:"1" == 1isFalse, 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.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.
Which way it goes
A two-option menu, with an
elsefor anything else. The choices are in quotes becauseinput()gives back a string.A decision inside a decision
An
ifcan go inside anotherif's block, indented one level further. This is called nesting. The inner question is only asked when the outer test wasTrue. If the outer test isFalse, Python skips the whole block, innerifand 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
elsebelongs to whichif. Anelsepairs with theifit lines up with. The usual mistake is pairing them by distance, with the nearestif, instead of by indent.Launch or wait?
The same launch rules, on a rainy day with almost no wind. Follow the indents to see which
elsebelongs to whichif.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 twoelse:lines: their indents decide whichifeach one belongs to.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
ifinside it. Run it with2and4hours: the first two hours cost 3.35, then 1.20 for each hour after, so 5.75.The
elseat the bottom catches anything that isn't on the menu: a3, 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.
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
1and type300. It should sayThat's 186.41 mph.Work out why every choice ends up at
No such option., then fix it.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,elifandelsefor the menu, with the choices in quotes, and anifinside option 3 for its second question. The checks try every option, sea level and high up, and choices that aren't on the menu.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.