25 — Calculator: asking and working it out
Everything asked for so far has been text. Text cannot be added up —
"12" and "4" together make 124. To get a number you have to say so.
Steps
Ask for a number.
ask numberreads the answer as a number:
run it →ask number first the first number ask number second the second number set result to first add second to result show resultWith
12and4you get16. Withoutnumberyou get124— two pieces of text stuck together.The four operations are four sentences, all of the shape "do this to that name":
run it →set result to 12 add 4 to result subtract 2 from result multiply result by 3 divide result by 2 show resultEach line changes the result in turn: 12 becomes 16, then 14, then 42, then 21. Only the last one reaches the screen, and it prints
21.0. Only division brings a decimal point. A division rarely comes out even, so it always answers with one.Which operation to do is something you ask. Four ways means
else if:
run it →set sign to times set result to 12 if sign equals plus add 4 to result else if sign equals times multiply result by 4 else show that is not an operation I know end show resultThe whole thing:
run it →ask number first the first number ask number second the second number ask sign one of plus minus times over set result to first if sign equals plus add second to result else if sign equals minus subtract second from result else if sign equals times multiply result by second else divide result by second end show result
Try it yourself
Add a fifth operation for the remainder — set result to the remainder of first divided by second is that one. Then answer 0 for the second number
and choose the division: the program stops with ZeroDivisionError. Guarding
it with if second equals 0 before dividing is the fix.
What you learned
ask number <name>reads the answer as a number; plainaskreads text.- The four operations are the sentences
add,subtract,multiply,divide. - Only division always answers with a decimal point.
- Three or more ways to go means
else ifbetween the first and the last.