Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
The way your code handles the variable i within the for loop seems to indicate a wrong understanding of the meaning of for i in range(1,n+2): range(1,n+2) will provide an object of type range. ...
Answer
#2: Post edited
- The way your code handles the variable `i` within the `for` loop seems to indicate a wrong understanding of the meaning of
- for i in range(1,n+2):
`range(1,n+2)` will provide an object of type `range`. This object represents the sequence of numbers from `1` to `n+2`. At the begin of each iteration the next element from that sequence is fetched and assigned to `i`, _no matter what happened to `i` in between_.- In other words, you can not "skip" elements from the `range` object just by incrementing `i` within the `for` loop. You can see this with the help of the following example code:
- for i in range(3):
- print("Value of i at start of loop body: i = ", i)
- i += 20
- print("Value of i after modification: i = ", i)
- The way your code handles the variable `i` within the `for` loop seems to indicate a wrong understanding of the meaning of
- for i in range(1,n+2):
- `range(1,n+2)` will provide an object of type `range`. This object represents the sequence of numbers from `1` to (not including) `n+2`. At the begin of each iteration the next element from that sequence is fetched and assigned to `i`, _no matter what happened to `i` in between_.
- In other words, you can not "skip" elements from the `range` object just by incrementing `i` within the `for` loop. You can see this with the help of the following example code:
- for i in range(3):
- print("Value of i at start of loop body: i = ", i)
- i += 20
- print("Value of i after modification: i = ", i)
#1: Initial revision
The way your code handles the variable `i` within the `for` loop seems to indicate a wrong understanding of the meaning of for i in range(1,n+2): `range(1,n+2)` will provide an object of type `range`. This object represents the sequence of numbers from `1` to `n+2`. At the begin of each iteration the next element from that sequence is fetched and assigned to `i`, _no matter what happened to `i` in between_. In other words, you can not "skip" elements from the `range` object just by incrementing `i` within the `for` loop. You can see this with the help of the following example code: for i in range(3): print("Value of i at start of loop body: i = ", i) i += 20 print("Value of i after modification: i = ", i)