I am trying to write a code that prints hello and a different number on each line. Sample output would look like this:
hi 21
hi 22
hi 23
etc. However I am getting a list index out of range error for my for loop.
Here is my code:
number = [21,22,23]
for x in number:
print('hi',number[x],'\n')
I am new to programming and any help would be greatly appreciated!
print('hi', x)xwill be21, the second time22and so forth. When you try to accessnumber[x], you're really accessingnumber[21]. Asnumberisn't long enough to have an index21, you're getting this error.