← Back to the Introduction to Computer Science outline The Course Maker
← Introduction to Computer Science outline
Week 7 · Assignment: Traditional alternative (no AI)

Week 7 — Functions That Hand Back Their Work — Traditional alternative (no AI)

Introduction to Computer Science Free sample edition
Traditional alternative (no AI), unpublished. In the course this is a separate assignment that ships unpublished beside the adaptive assignment: same objective, no AI. An instructor who prefers it publishes it and unpublishes or deletes the adaptive one, as the note at its top says.

Instructors: publish this instead of the adaptive version if you prefer, and unpublish or delete the adaptive one.

Assignment 7 of the term — predict what a function hands back, see why argument order matters, tell print from return, and explain it all to someone who has never programmed.

This week you learned that a function is defined once with def and runs only when called, that arguments bind to parameters by position, that return hands a value back while print only shows it, and that a function's local names vanish when the call ends. In this assignment you trace four short problems, run each one to check your prediction, run two more programs that show a printing function's None and a stored return value, and explain a real mix-up to someone who has never programmed. This builds on Weeks 5 and 6. This week's lecture tutorial, quiz, discussion, and Coding Lab practise the same ideas from other angles, and this is the last assignment before next week's midterm review.

The task

A neighbor who runs a community garden wrote a short program to count the plots still free and add 5 new ones the garden is building:

def plots_left(total, rented):
    print(total - rented)

left = plots_left(40, 25)
print(left + 5)

It printed 15 and then stopped with an error. Your neighbor says, "Python lost my number." Your job is to check what Python really did, the way a programmer would: predict each output before you run it, run it in a free browser Python runner, and explain what happened. All four problems are illustrative; Problems 1 and 2 use this week's reading log, whose goal is 300 pages with 45 pages read at the start.

Work these four problems. For each one, write your prediction first, then run the code and write the actual output beside your prediction:

  1. The reading log's function def pages_left(goal, pages_read): returns goal - pages_read and is called as print(pages_left(300, 45)). What does the program print?
  2. The same function is called as print(pages_left(45, 300)), with the arguments in the other order. What does the program print?
  3. Your neighbor's function is fixed to return total - rented; the program runs left = plots_left(40, 25) and then print(left + 5). What does it print?
  4. The temperature converter def to_fahrenheit(celsius): returns celsius * 9 / 5 + 32 and is called as print(to_fahrenheit(20)). What does the program print?

Beside each answer, add one brief interpretation:

  • Problem 1: state which value goal and which value pages_read hold during the call, and name the two local names that vanish when the call ends.
  • Problem 2: explain why swapping the arguments changes the printed value, and name the kind of error this slip is.
  • Problem 3: run your neighbor's original printing version, report what it prints and the exception type on the last line of its error, and explain why changing print to return fixes it.
  • Problem 4: explain why the printed value ends in .0, then store the returned value with reading = to_fahrenheit(20), run print(reading), and report what prints.

Explain it to a non-programmer

Write a 3–5 sentence message to your neighbor that explains, in everyday words, why Python seemed to lose the number and how changing one word in the function fixes it. Use the idea that showing a value on the output is different from handing it back to the rest of the program, and name one limit or caveat, such as what the program must now do to display the count. Explain any technical term in everyday words. More than one good message is possible.

For a different, ungraded example, imagine a roommate whose bill-splitting function takes the bill first and the number of people second, but who called it with the number of people first. Model message: "Your function fills its two slots in order: the first value you give it goes into the bill slot, and the second goes into the people slot. You handed it the people count first, so it treated the number of people as the bill and the bill as the number of people. Python did exactly what the order said, which is why there was no error message, only a strange amount. One catch: Python cannot warn you about this kind of mix-up, so read the first line of the function before you call it."

Use that pattern for your neighbor'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 value each parameter held or what each call handed back.

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 your neighbor's original printing version and of print(reading), each with what printed; and
  • your message to your neighbor, explaining why the number seemed lost, how changing one word fixes it, 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