dolphins123
Badges: 10
Rep:
?
#1
Report Thread starter 1 year ago
#1
i dont really understand this code for programming arrays in python. i dont understand the 'range' or 'index' bit, what impact does it have on the program?

full program:

numbers=[0,0,0,0]
for index in range(4):
numbers[index]=input("Enter an integer number: ")
n = int(numbers[index])

print("first number =",numbers[0])
print("first number=",numbers[3])
Last edited by dolphins123; 1 year ago
0
reply
fbacc
Badges: 10
Rep:
?
#2
Report 1 year ago
#2
range (4) means all the integers from 0 to 4 so, 0,1,2,3,4

https://www.pythoncentral.io/pythons...ion-explained/

index is the value with 0,1,2,3,4 within each iteration of the loop.

So first time around it will be 0, next 1 etc until 4.

I think your code should be range (3) as this will produce 4 values 0,1,2,3. Array indexes start at 0 confusing I know.

The value of index is used in your code to get the value out of the array numbers[] relating to that index value. Although in your code you have set all of the values to 0?

your code at the end should really read

print("first number =",numbers[0])

print("fourth number=",numbers[3])
Last edited by fbacc; 1 year ago
0
reply
winterscoming
Badges: 19
Rep:
?
#3
Report 1 year ago
#3
(Original post by fbacc)
range (4) means all the integers from 0 to 4 so, 0,1,2,3,4
This isn't right I'm afraid, and also doesn't match the link you've pasted.

range(4) means a range of 4 numbers starting from zero. The sequence it produces is 0,1,2 and 3.

(Original post by fbacc)
range (4) means all the integers from 0 to 4 so, 0,1,2,3,4
I think your code should be range (3) as this will produce 4 values 0,1,2,3. Array indexes start at 0 confusing I know.
Again, that's not quite right.
range(4) will produce 4 integers starting at 0 -- i.e. not including the integer 4, because that would be a total of 5 integers.
range(3) will produce 3 integers starting at 0 -- i.e. not including the integer 3, because that would be a total of 4 integers

The code in the original post works fine anyway, if the code had overstepped beyond the end of the list, then Python would have churned up an error saying "IndexError: list index out of range".

There's a good lesson on loops and ranges in Python here: https://www.py4e.com/lessons/loops
Last edited by winterscoming; 1 year ago
0
reply
fbacc
Badges: 10
Rep:
?
#4
Report 1 year ago
#4
(Original post by winterscoming)
This isn't right I'm afraid, and also doesn't match the link you've pasted.

range(4) means a range of 4 numbers starting from zero. The sequence it produces is 0,1,2 and 3.


Again, that's not quite right.
range(4) will produce 4 integers starting at 0 -- i.e. not including the integer 4, because that would be a total of 5 integers.
range(3) will produce 3 integers starting at 0 -- i.e. not including the integer 3, because that would be a total of 4 integers

The code in the original post works fine anyway, if the code had overstepped beyond the end of the list, then Python would have churned up an error saying "IndexError: list index out of range".

There's a good lesson on loops and ranges in Python here: https://www.py4e.com/lessons/loops
Oops your right
1
reply
winterscoming
Badges: 19
Rep:
?
#5
Report 1 year ago
#5
(Original post by fbacc)
Oops your right
Don't worry, definitely a common one to get mixed up
0
reply
dolphins123
Badges: 10
Rep:
?
#6
Report Thread starter 1 year ago
#6
(Original post by winterscoming)
This isn't right I'm afraid, and also doesn't match the link you've pasted.

range(4) means a range of 4 numbers starting from zero. The sequence it produces is 0,1,2 and 3.


Again, that's not quite right.
range(4) will produce 4 integers starting at 0 -- i.e. not including the integer 4, because that would be a total of 5 integers.
range(3) will produce 3 integers starting at 0 -- i.e. not including the integer 3, because that would be a total of 4 integers

The code in the original post works fine anyway, if the code had overstepped beyond the end of the list, then Python would have churned up an error saying "IndexError: list index out of range".

There's a good lesson on loops and ranges in Python here: https://www.py4e.com/lessons/loops
thank you, the code is much more clearer now, i'll use the link to help me.
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

What support do you need with your UCAS application?

I need help researching unis (4)
8%
I need help researching courses (5)
10%
I need help with filling out the application form (3)
6%
I need help with my personal statement (24)
48%
I need help with understanding how to make my application stand out (10)
20%
I need help with something else (let us know in the thread!) (1)
2%
I'm feeling confident about my application and don't need any help at the moment (3)
6%

Watched Threads

View All