Week 3 — Typed Text, Built Lines, and Sliced Strings — Traditional alternative (no AI)
Instructors: publish this instead of the adaptive version if you prefer, and unpublish or delete the adaptive one.
Assignment 3 of the term — read typed input as text and convert it, build an output line with an f-string, measure a slice and a string's last index, and explain it all to someone who has never programmed.
This week you learned that input() always hands back a string, built output lines with f-strings, and took a string apart by index and by slice. In this assignment you trace four short problems, run each one to check your prediction, run two more lines that show the strict index and the forgiving slice, and explain a real mix-up to someone who has never programmed. This builds on Week 2's type conversion. This week's lecture tutorial, quiz, discussion, and Coding Lab practise the same ideas from other angles.
The task
A relative is building a small order program for a bake-sale table. It asks cookies = input("Cookies: ") and then extra = input("Extra: "), and when a customer typed 3 and then 1, the line print(cookies + extra) printed 31. Your relative says, "Python can't even add 3 and 1." Your job is to check what Python really did, the way a programmer would: predict each result before you run it, run it in a free browser Python runner, and explain what happened. All four problems are illustrative and use this week's reading log, where book = "the river book" and pages_read = 45.
Work these four problems. For each one, write your prediction first, then run the code and write the actual output beside your prediction:
- The user types 25, and the program stores it as the string
typed = "25". What doesprint(int(typed) + 1)print when it runs? - The reading log holds
pages_read = 45, and the user types 30, stored aspages_today = "30". What number appears in the lineprint(f"{pages_read + int(pages_today)} pages read so far")? - With
book = "the river book", how many characters does the slicebook[4:9]keep? - With
book = "the river book",print(len(book))prints 14. What is the index of the last character ofbook?
Beside each answer, add one brief interpretation:
- Problem 1: name
input()handing back a string as the reasonint()is needed, and say whatprint(typed * 2)prints instead, and why. - Problem 2: explain what the
fin front of the string does, and say what happens whenpages_readis joined to the text with+instead: runprint(pages_read + " pages read so far")and read the last line of its error message. - Problem 3: say what
print(book[4:9])prints, and name which index the slice leaves out and why. - Problem 4: say what
print(book[13])prints. Then runprint(book[14])andprint(book[4:99]), and explain why one raises an IndexError while the other runs.
Explain it to a non-programmer
Write a 3–5 sentence message to your relative that explains, in everyday words, why print(cookies + extra) printed 31 and how int() would make it print 4. Use the idea that typed text stays text until the program converts it, and name one limit or caveat, such as what would happen if a customer typed the word three instead of the digit. Explain any technical term in everyday words. More than one good message is possible.
For a different, ungraded example, imagine a teammate whose program was supposed to show the first letter of the name Jordan on a badge, but print(name[1]) showed o. Model message: "Python numbers the letters in a name starting from 0, not 1, so position 1 is the second letter, o. The first letter, J, sits at position 0, so the line should ask for name[0]. The program did exactly what it was told; it counts from zero every time. One catch: this fixes the first letter, but a short badge that cuts long names will still need a separate decision about how many letters to show."
Use that pattern for your relative's question, with your own wording and evidence.
Show your traces. A reader should be able to follow every prediction step by step and see the actual output of your run beside it, without guessing which values, indexes, or conversions you used.
Deliverables
Submit a single report containing:
- the four answers, each with your prediction, the step-by-step trace, the actual output of your run, and the brief interpretation the task names for that problem;
- the runs of
print(typed * 2),print(pages_read + " pages read so far"),print(book[14]), andprint(book[4:99]), each with what printed or the error type from the last line of the error message; and - your message to your relative, explaining why the program printed 31, how
int()would make it print 4, and one limit.
Keep it to roughly one page. Clear and correct beats long.
Choose how you show it. Present your report as written text, or as a recorded audio or video explanation of about three minutes with a transcript, or as labelled screen captures of your runs with a short note for each. Every form is graded by the same scoring criteria. Submit in any accessible file type or as a link; a recorded response may be submitted as audio or video with a transcript, or as text.
How you'll be graded
The full analytic rubric is attached to this assignment in Canvas.
Academic integrity and AI use
This version is designed to be completed without AI: make the predictions, run the code, and write the interpretations and the message yourself, without any AI tool. You may use a free browser Python runner, Python Tutor, and the official Python documentation. Submitting AI-generated work as your own, or copying another student's work, is an integrity violation under the course policy.
Built with The Course Maker · samples.thecoursemaker.com