Assignment 2 — Conditionals
Programs that branch. Small, because Week 2 is a single class.
Due Sep 8, 2026
There is no class between the day this goes out and the day it is due. Labor Day takes out Monday, so the next time we meet is 09/09 — the day after the deadline. Two things follow, and both are in your favour if you act on them early.
- Problem 1 is done in class on Wednesday, and problem 2 is started there. If you leave Wednesday with those running, the rest is a weekend’s work.
- Discord
#helpis the class this week. Ask there. I answer it, and a question asked on Thursday is worth a great deal more than the same question asked on Monday night.
AI: the course AI policy applies in full. No AI-generated code, and no asking a chatbot to solve these problems before the deadline. The optional problems say when that changes.
Starting code
Everything below starts from this. Copy it into a fresh Pyret file called a2-stripes.arr — do not retype it from the board, and do not start from your A1 file.
unit = 20
fun stripe(u, color):
if u < 10:
rectangle(u * 6, u, "solid", color)
else:
rectangle(u * 6, u, "outline", color)
end
end
beside(stripe(4, "red"), stripe(unit, "red"))Run it and look at the two stripes. The small one is filled in and the big one is a rectangle you can see through, and the reason is the whole point of the assignment: an outline drawn on a four-pixel stripe has nothing left to show you. Below a certain size, you have to draw the thing differently.
if is what lets one function do that. The reading is [DCIC] 3.4 — Conditionals and Booleans, and §3.4.5 in particular is worth having open while you work — it is the step-by-step version of what we did on paper.
Part 1 — the branch, and the examples that pin it down
Write the
where:block forstripe, and write it first. Four examples, and they are the point of this problem rather than a formality.- Two on each side of the rule, so that both branches are actually tested.
- One of them must be
u = 10itself. Ten is the boundary, it is the one place where<and<=disagree, and it is the example that would catch you having written the wrong one. - Work each answer out yourself and write the answer down. An example copied from the body agrees with the body no matter what the body says.
An example here is an image, which is legal and probably not what you expect:
where: stripe(10, "red") is rectangle(60, 10, "outline", "red")Then change
<to<=in the body, press Run, and check that exactly one example goes red. If none of them does, your examples are not doing their job yet — fix them before you go on. Change it back afterwards.Add a third case with
else if. Very large stripes should be drawn differently again: fromu = 30up, draw the stripe as an outline and make the colour"black"regardless of what was asked for, so it is obvious which case you are in.So there are three cases now: below 10, from 10 up to but not including 30, and 30 and above. Add at least two more examples, and make sure one of them is
u = 30.- Write it as one
if/else if/else, not as two separateifexpressions in a row. - This is [DCIC] 3.4 — Conditionals and Booleans §3.4.4, and it is worth reading the three-level
add-shippingthere before you start.
- Write it as one
Part 2 — asking two questions at once
Add a Boolean parameter. Give
stripea third parameter,formal, so that it is calledstripe(20, "red", true).A formal flag is always drawn as an outline, whatever the size. So the stripe is an outline if it is
formal, or if it is big enough for one to show.- Write that as one question using
or, not as anifinside anotherif. You are looking for something close toif formal or (u >= 10):. formalis alreadytrueorfalse. You do not need to writeformal == true, and it is worth understanding why not.- You now have two things that can vary, so the examples have to vary both. Six examples, at least: each branch, with
formaltrue and false.
- Write that as one question using
Put it back in your flag. Take the flag you wrote for A1 and turn it into
fun flag(unit, formal), using your newstripewherever it drew a rectangle.- Then produce one picture showing your flag at two sizes, formal and not, side by side — four flags, one expression, no copy-paste.
- If your A1 flag never became a function, that is fine and it is not graded twice. Do this step with a flag made of three stacked stripes instead, and say so in a comment.
Submit
a2-stripes.arron Brightspace. One file. Make sure it runs from top to bottom before you submit — a file that does not run gets no credit for the parts that would have worked.
Grading
Full credit for a file that runs and does 1, 2, 3 and 4.
Most of the weight is on two things, because they are what the week is about:
- The examples, in problems 1 and 3 — both branches covered, the boundary case present, and the answers worked out rather than copied from the body.
- One question rather than two, in problem 3 —
orin a singleif, not a nested one.
Presentation
Two of you will be asked on Wednesday, September 9 to put your file on the projector and walk the class through it — what your examples are, and why those and not others. You will not be told in advance.
A solution that does not fully work is still worth presenting. Explaining what you wrote and where you think it goes wrong is a real presentation and is graded as one.
What else to do (optional part)
Ungraded. No points, and skipping them costs nothing.
Write problem 3 the other way, as an
ifnested inside anelse, and keep both versions in the file with one commented out. Then, in a comment: the theatre changes its mind and outlines become solid. Which version would you rather be holding? That is [DCIC] 3.4 — Conditionals and Booleans §3.4.7’s own question.What does
stripe(10, "red")give you if you write the rule asu <= 10instead ofu < 10? Answer it on paper first, by reducing the expression the way we did in class, and only then run it. If the paper and the screen disagree, the interesting part is finding which step you got wrong.Add a
size-namefunction that takes auand gives back the string"small","medium"or"large"using the same three cases. Then notice that you have now written the same set of questions twice, in two functions. Write one sentence about what would go wrong if you later changed the boundary from 30 to 40.[Consulting AI allowed, after the deadline only] Ask a chatbot what
andandordo when the left-hand side already settles the answer — the phrase to ask about is short-circuit evaluation. Then say in a comment whether that changes anything about theoryou wrote in problem 3.