Repair a loop
A running total misses its final value. Find the boundary error and use small examples to check your fix.
EduKateby Cass Education & Careers ServicesTry three short Python missions in your browser. Predict what the code will do, fix one problem and test your reasoning. Each activity includes a hint and a complete worked solution.
Your program runs in a separate browser workspace on this device. Python downloads when you first press Run or Check, so an internet connection is needed. Code is kept only while this page is open; copy anything you want to keep before leaving. Use the supplied examples or code you understand.

A running total misses its final value. Find the boundary error and use small examples to check your fix.
Accept whole-number scores from 0 to 100. Handle invalid text and test both ends of the allowed range.
A search gives up too soon. Correct its control flow and test first, last, missing and repeated values.
Complete total_up_to(n) so it returns the total of the whole numbers from 1 up to and including n. Assume n is a whole number from 0 to 100. For n = 0, return 0.
For example, total_up_to(5) should return 15 because 1 + 2 + 3 + 4 + 5 = 15. Run the starter code first. Which value is missing?
The stop value in range(start, stop) is not included. range(1, 5) produces 1, 2, 3 and 4. What stop value would include 5?
def total_up_to(n):
total = 0
for value in range(1, n + 1):
total += value
return total
print(total_up_to(5))Increasing the stop value to n + 1 makes the final included value n. When n is zero, range(1, 1) has no values and the initial total of zero is returned.
Try a variation: change the loop to total only the even numbers up to n. Predict the answer for 6 before running it. The mission checker tests the original task, so use your own print examples for this variation.
Complete valid_score(text). Return True if the text can be converted to a whole number from 0 to 100 inclusive; otherwise return False. Surrounding spaces are allowed. Decimal text such as "8.5" is not a whole-number input.
The starter accepts "101" and raises an error for "hello". Fix both problems. Keep the function name and return a Boolean, rather than printing the answer inside the function.
int(text) raises ValueError when text cannot be converted to an integer. Use try and except ValueError to return False for that case. Then check both the minimum and maximum.
def valid_score(text):
try:
score = int(text)
except ValueError:
return False
return 0 <= score <= 100
print(valid_score("42"))
print(valid_score("101"))
print(valid_score("hello"))The conversion establishes a whole-number value. The chained comparison checks the allowed range. Conversion failure returns False instead of ending the program. Validating a value does not prove that it is accurate: a score of 42 could still have been mistyped.
Try a variation: add a maximum parameter and test a quiz scored out of 20. Include 0, 20, 21 and invalid text in your own tests. The mission checker continues to test the original 0–100 task.
Complete search_first(values, target). Return the index of the first match, or -1 if the target is missing. Indexes start at zero. An empty list must also return -1.
Searching [18, 7, 26, 11, 4] for 11 should return 3. The starter returns -1 after checking only the first item. Trace its indentation and find out why.
A return inside the loop ends the function during that iteration. The failure result should happen only after every possible position has been checked. Which line needs to move one indentation level left?
def search_first(values, target):
for index in range(len(values)):
if values[index] == target:
return index
return -1
print(search_first([18, 7, 26, 11, 4], 11))The successful return remains inside the match condition. The failure return is after the loop, so it runs only if no match caused an earlier return. An empty list has zero loop iterations and reaches -1 immediately. Repeated values return the first match.
Try a variation: write a second function that returns all matching indexes. For [7, 2, 7] and target 7, the result should be [0, 2]. Use your own examples; the mission checker tests the first-match function.
Keep the mission’s function name. Edit the code, then use Run for output or Check mission for example tests. Use spaces for indentation; the Tab key moves to the next control. These missions use function arguments rather than interactive input().
Preparing your workspace…
Your program output will appear here.
Small programs only: execution stops after 8 seconds and output is limited to 12,000 characters. Stop keeps your code and resets Python. Example tests guide learning; they are not an exam mark or a complete proof of correctness.
Describe the original fault, the change you made and one test that distinguishes the faulty version from the corrected one. Then try the same idea with different values.
Try the free learning games or explore the subscribed Computer Science courses for a larger mission library and the full course IDE.