-
Notifications
You must be signed in to change notification settings - Fork 27
Done with the functions #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,198 @@ | ||||||||||||||||||||||||||||||||||||
| """Learning Outcome: Functions""" | ||||||||||||||||||||||||||||||||||||
| def sum_of_squares(n: int): | ||||||||||||||||||||||||||||||||||||
| for i in range(1, n + 1): | ||||||||||||||||||||||||||||||||||||
| sum = i ** 2 | ||||||||||||||||||||||||||||||||||||
| return sum | ||||||||||||||||||||||||||||||||||||
| if n < 0: | ||||||||||||||||||||||||||||||||||||
| raise ValueError("n must be a postive integer") | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+3
to
+7
|
||||||||||||||||||||||||||||||||||||
| for i in range(1, n + 1): | |
| sum = i ** 2 | |
| return sum | |
| if n < 0: | |
| raise ValueError("n must be a postive integer") | |
| if n < 0: | |
| raise ValueError("n must be a postive integer") | |
| for i in range(1, n + 1): | |
| sum = i ** 2 | |
| return sum |
Copilot
AI
Oct 27, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corrected spelling of 'postive' to 'positive'.
| raise ValueError("n must be a postive integer") | |
| raise ValueError("n must be a positive integer") |
Copilot
AI
Oct 27, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The empty list check is unreachable because it appears after the return statement. Move this validation to the beginning of the function to prevent division by zero on line 10.
| avg = sum(grades) / len(grades) | |
| return "Pass" if avg >= min_pass else "Fail" | |
| if not grades: | |
| return "Fail" | |
| if not grades: | |
| return "Fail" | |
| avg = sum(grades) / len(grades) | |
| return "Pass" if avg >= min_pass else "Fail" |
Copilot
AI
Oct 27, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Variable avg is undefined; should be average to match the variable defined on line 16.
| if score < avg: | |
| if score < average: |
Copilot
AI
Oct 27, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The empty dictionary check is unreachable because it appears after the return statement. Move this validation to the beginning of the function to prevent division by zero on line 16.
| average = sum(scores.values()) / len(scores) | |
| below_average = [] | |
| for subject, score in scores.items(): | |
| if score < avg: | |
| below_average.append(subject) | |
| return {"average": average, "below_average_subjects": below_average} | |
| if not scores: | |
| return {"average": 0, "below_average_subjects": []} | |
| if not scores: | |
| return {"average": 0, "below_average_subjects": []} | |
| average = sum(scores.values()) / len(scores) | |
| below_average = [] | |
| for subject, score in scores.items(): | |
| if score < average: | |
| below_average.append(subject) | |
| return {"average": average, "below_average_subjects": below_average} |
Copilot
AI
Oct 27, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function get_score is undefined. Replace with a lambda function: key=lambda x: x[1] to extract the score from each tuple.
| sorted_students = sorted(students, key=get_score, reverse=True) | |
| sorted_students = sorted(students, key=lambda x: x[1], reverse=True) |
Copilot
AI
Oct 27, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function returns only the first even number instead of a list of all even numbers. Initialize an empty list before the loop, append even numbers to it, and return the list after the loop.
| for i in range(1, n + 1): | |
| if i % 2 == 0: | |
| return i | |
| pass | |
| evens = [] | |
| for i in range(1, n + 1): | |
| if i % 2 == 0: | |
| evens.append(i) | |
| return evens |
Copilot
AI
Oct 27, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function returns only the first odd number instead of a list of all odd numbers. Initialize an empty list before the loop, append odd numbers to it, and return the list after the loop.
| for i in range(1, n + 1): | |
| if i % 2 != 0: | |
| return i | |
| odds = [] | |
| for i in range(1, n + 1): | |
| if i % 2 != 0: | |
| odds.append(i) | |
| return odds |
Copilot
AI
Oct 27, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function returns only the first number that isn't n instead of a list of all numbers except n. Initialize an empty list before the loop, append qualifying numbers to it, and return the list after the loop.
| for i in range(1, length + 1): | |
| if i != n: | |
| return i | |
| result = [] | |
| for i in range(1, length + 1): | |
| if i != n: | |
| result.append(i) | |
| return result |
Copilot
AI
Oct 27, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function returns only the square of the first number instead of a list of all squares. Initialize an empty list before the loop, append each square to it, and return the list after the loop.
| for num in nums: | |
| square = num ** 2 | |
| return square | |
| pass | |
| squares = [] | |
| for num in nums: | |
| squares.append(num ** 2) | |
| return squares |
Copilot
AI
Oct 27, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The empty list check is unreachable because it appears after the return statement. Move this validation to the beginning of the function to prevent division by zero on line 129.
| total = sum(nums) | |
| average = total / len(nums) | |
| return (total, average) | |
| if not nums: | |
| return (0, 0) | |
| if not nums: | |
| return (0, 0) | |
| total = sum(nums) | |
| average = total / len(nums) | |
| return (total, average) |
Copilot
AI
Oct 27, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function returns only the first even number instead of a list of all even numbers. Initialize an empty list before the loop, append even numbers to it, and return the list after the loop.
| for num in nums: | |
| if num % 2 == 0: | |
| return num | |
| pass | |
| even_nums = [] | |
| for num in nums: | |
| if num % 2 == 0: | |
| even_nums.append(num) | |
| return even_nums |
Copilot
AI
Oct 27, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line 166 incorrectly calls the input() built-in function which prompts for user input, overwriting the function parameter. Initialize reversed_text as an empty string instead: reversed_text = ''.
| reversed_text = input("Enter Text: ") | |
| reversed_text = '' |
Copilot
AI
Oct 27, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The empty list check is unreachable because it appears after the return statement. Move this validation to the beginning of the function to prevent an IndexError on line 174.
| largest = nums[0] | |
| for num in nums: | |
| if num > largest: | |
| largest = num | |
| return largest | |
| if not nums: | |
| return None | |
| pass | |
| if not nums: | |
| return None | |
| largest = nums[0] | |
| for num in nums: | |
| if num > largest: | |
| largest = num | |
| return largest |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function only returns the square of the last number instead of the sum of all squares. Initialize
sum = 0before the loop and usesum += i ** 2inside the loop to accumulate the total.