# Print first 10 natural numbers
print ("Enter the number:")
Num = int(input(" "))
x=1
Natural= [ ]
print (Natural)
if Num>=1:
while x<=Num:
Natural.append(Natural[x])
x=x+1
print (Natural)
else:
print('Please Enter a valid number')
Gives Error below:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Input In [67], in <cell line: 8>()
8 if Num>=1:
9 while x<=Num:
---> 10 Natural.append(Natural[x])
11 x=x+1
12 print (Natural)
IndexError: list index out of range
Welcome!
To make your code more readable you can:
try to edit your post(pencil icon)
press ctrl-e
You will see what I am talking about when do this couple of steps)
Your code can look like:
this
...
...
...
What do you think is going on in these lines of code?
General advice. If you are Python-beginner, you definitely wanna know about
Python Tutor.
Check it out, try to test your code there.
This did not help, could anyone tell me where’s the mistake?
When your code runs, the first time this line is encountered, the value of x
is 1
. Since at this point in the program, Natural
is an empty array, when your reference Natural[x]
, you are attempting to reference Natural[1]
. Since Natural
is an empty array, there will be no element at index 1
, hence the error message.
I think I know what you want to accomplish, but explain what you were hoping this line would do for you.
May I clarify? I believe it’s called a list in Python?
someList = []
Hi Randell,
Thanks for your reply.
I wanted to print first n natural numbers , just a basic program and put those Natural numbers to a list by appending them gradually through the loop.
That line was to append the natural numbers.
Regards,
Suresh
Example for you. Take a look at appending. Name of the list is not present in the parenthesis.
>>> numbers = [1, 2, 3]
>>> numbers.append(4)
>>> numbers
[1, 2, 3, 4]
# Print first 10 natural numbers
print ("Enter the number:")
Num = int(input(" "))
x=1
Natural= [ ]
print (Natural)
if Num>=1:
while x<=Num:
Natural.append(x)
x=x+1
print (Natural)
else:
print('Please Enter a valid number')
In the line:
Natural.append(Natural[x])
you should instead do
Natural.append(x)
This would make it correct, if you want explanation please mention
yes you are absolutely corect
Welcome to the forum.
Your explanation is cool, but consider this:
In this community it is not considered the best practice to provide direct answers like this:
When helping others,
Try to provide some hints, or similar examples, or maybe links on relevant sources like MDN
ohh really sorry, I created my account just a couple of minutes ago…
I should’ve read the guidelines before posting,
This would not happen again…
Hi Vinay, Could you explain why Natural.append(Natural) won’t work?
Hey
So we have a list
Natural = [ ]
How many elements does it have?
What would happen if i try to print Natural[0] ?
What would happen if i try to print Natural[1] ?
What is the value of x in the beginning?
What if try to print Natural[ x ]?
Answer these questions and you will find your explanation on your own.
If not please don’t hesitate to ask again.
Got it thank you Vinay …
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.