Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Off-by-One and Range Errors | Common Logic and Comparison Errors
Common Python Mistakes and How to Fix Them

bookOff-by-One and Range Errors

123
numbers = [2, 4, 6, 8, 10] for i in range(0, len(numbers) - 1): print(numbers[i])
copy

Off-by-one errors are a frequent source of bugs in Python, especially when working with loops and slicing. Python's range function generates a sequence that starts at the first argument and stops before the second argument. In the code sample above, range(0, len(numbers) - 1) produces indices from 0 up to, but not including, len(numbers) - 1. If numbers has 5 elements, this means the loop iterates over indices 0 to 3, missing the last element at index 4. This kind of mistake often happens when you subtract one from the length, thinking it will include the last item, but since range is exclusive at the stop value, it actually skips it. To loop over every element, you should use range(len(numbers)) or simply iterate directly over the list with for item in numbers:. Always double-check the start and stop values in your ranges to avoid accidentally omitting or repeating elements.

1. Which of the following ranges will ensure that every element in a list named values is printed exactly once using a for loop over indices?

2. Rearrange the parts of the following for loop so that it prints every element in the list items exactly once.

question mark

Which of the following ranges will ensure that every element in a list named values is printed exactly once using a for loop over indices?

Select the correct answer

question-icon

Rearrange the parts of the following for loop so that it prints every element in the list items exactly once.

items = ["a", "b", "c", "d"] for i in range(0, ): print(items[i])
"a"
"b"
"c"
"d"

Click or drag`n`drop items and fill in the blanks

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 2

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

Can you explain more about off-by-one errors in other contexts?

What are some best practices to avoid off-by-one errors in Python?

Can you show how to correctly loop through all elements in the list?

Awesome!

Completion rate improved to 5.26

bookOff-by-One and Range Errors

Sveip for å vise menyen

123
numbers = [2, 4, 6, 8, 10] for i in range(0, len(numbers) - 1): print(numbers[i])
copy

Off-by-one errors are a frequent source of bugs in Python, especially when working with loops and slicing. Python's range function generates a sequence that starts at the first argument and stops before the second argument. In the code sample above, range(0, len(numbers) - 1) produces indices from 0 up to, but not including, len(numbers) - 1. If numbers has 5 elements, this means the loop iterates over indices 0 to 3, missing the last element at index 4. This kind of mistake often happens when you subtract one from the length, thinking it will include the last item, but since range is exclusive at the stop value, it actually skips it. To loop over every element, you should use range(len(numbers)) or simply iterate directly over the list with for item in numbers:. Always double-check the start and stop values in your ranges to avoid accidentally omitting or repeating elements.

1. Which of the following ranges will ensure that every element in a list named values is printed exactly once using a for loop over indices?

2. Rearrange the parts of the following for loop so that it prints every element in the list items exactly once.

question mark

Which of the following ranges will ensure that every element in a list named values is printed exactly once using a for loop over indices?

Select the correct answer

question-icon

Rearrange the parts of the following for loop so that it prints every element in the list items exactly once.

items = ["a", "b", "c", "d"] for i in range(0, ): print(items[i])
"a"
"b"
"c"
"d"

Click or drag`n`drop items and fill in the blanks

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 2
some-alt