Crediting code from other sources in comments

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Crediting code from other sources in comments

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which comment correctly credits reused code?
A # Source: https://example.com/sort, Jo Lin, MIT licence
B # sorting code
2. Where should the # Source: comment go?
A Hidden at the very end of the file
B Directly above the reused code
3. You copy a function from a tutorial website. What should you do?
A Add a comment with the URL, author and licence
B Delete the URL so the code looks like yours
4. Which detail is MISSING from this comment? # Source: https://example.com/timer
A The author and the licence
B The programming language
5. Why do you mark which lines are not your own?
A So the computer runs the reused lines faster
B So your teacher can see which code you wrote and which you reused

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1# Source: https://example.com/sort-tutorial A# Source: Real Python, author: A. Lee, licence: CC BY 4.0 def bubble_sort(items):
2# Author: J. Chen | Licence: MIT BThe credit comment is placed below the reused code instead of above it, so it is easy to miss which lines it refers to.
3Credit reused code by placing a comment above it naming the source, author and licence CA comment that records the URL where the reused code was found, so the original source can be traced.
4Mark which lines are not your own so your work is clearly separated from borrowed code D# --- Start of code reused from library docs --- # --- End of reused code ---
5# Found this online total = sum(prices) EThe comment does not name the source, the author or the licence, so the original work is not properly credited.
6def area(r): return 3.14 * r * r # Source: https://example.com/area (Ms Wu, CC0) FA comment that names the person who wrote the reused code and the licence that allows you to use it.

Definitions

# Source:

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
_ Source: https://example.com/sort
2.
# ______: https://example.com/sort
3.
# Source: https://example.com/sort # ______: Sam Lee # Licence: MIT
4.
# Reused code: _____ total = sum(nums) # Reused code: end
5.
# Source: https://example.com/sort # Author: Sam Lee # _______: MIT

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A # Source: example.com/sort
B # Author: Sam Lee
C Licence: MIT
D def sort_list(items):
E return sorted(items)
Question 2
A name = input('Name: ')
B # Source: github.com/ana/tools
C # Author: Ana Ruiz, MIT
D # Lines below are my own work
E print(name.title())
Question 3
A # Source: I wrote this
B # Author: Kim Tran, CC BY-SA
C total = sum(scores)
D print(total)
Question 4
A // Source: tutorials.com/loop
B # Author: Sara Kim
C # Licence: CC BY 4.0
D for i in range(5):
E print(i)
Question 5
A // Source: github.com/jo/utils
B // Author: Jo Park
C # Licence: MIT
D function double(n) {
E return n * 2;
F }

Answer Key

Crediting code from other sources in comments
Section A: Quick Questions
Q1
A — # Source: https://example.com/sort, Jo Lin, MIT licence
Q2
B — Directly above the reused code
Q3
A — Add a comment with the URL, author and licence
Q4
A — The author and the licence
Q5
B — So your teacher can see which code you wrote and which you reused
Section B: Match the Pairs
Q1
# Source: https://example.com/sort-tutorial → A comment that records the URL where the reused code was found, so the original source can be traced.
Q2
# Author: J. Chen | Licence: MIT → A comment that names the person who wrote the reused code and the licence that allows you to use it.
Q3
Credit reused code by placing a comment above it naming the source, author and licence → # Source: Real Python, author: A. Lee, licence: CC BY 4.0 def bubble_sort(items):
Q4
Mark which lines are not your own so your work is clearly separated from borrowed code → # --- Start of code reused from library docs --- # --- End of reused code ---
Q5
# Found this online total = sum(prices) → The comment does not name the source, the author or the licence, so the original work is not properly credited.
Q6
def area(r): return 3.14 * r * r # Source: https://example.com/area (Ms Wu, CC0) → The credit comment is placed below the reused code instead of above it, so it is easy to miss which lines it refers to.
Section C: Definitions
Q1
# Source: — A comment placed above code you reused that names the source (such as a URL), the author and the licence, so the original work is credited and your own lines are clear.
Section D: Fill in the Blanks
Q1
# Source: https://example.com/sort
Q2
# Source: https://example.com/sort
Q3
# Source: https://example.com/sort # Author: Sam Lee # Licence: MIT
Q4
# Reused code: start total = sum(nums) # Reused code: end
Q5
# Source: https://example.com/sort # Author: Sam Lee # Licence: MIT
Section E: Spot the Error
Q1
C — The licence line has no # at the start, so Python reads it as code and gives a syntax error. It should be # Licence: MIT.
Q2
D — The lines below were reused, so the comment must say they are not your own. It should be # Lines below are not my own.
Q3
A — A Source comment must name where the code came from, such as a URL. Claiming you wrote it does not credit the original. It should be # Source: stackoverflow.com/1.
Q4
A — // does not start a comment in Python, so this line causes an error. Python comments start with #, so it should be # Source: tutorials.com/loop.
Q5
C — This program is JavaScript, where # does not start a comment. The licence line must use //, like the other credit lines: // Licence: MIT.