0

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!

3
  • 1
    Change your print call to: print('hi', x) Commented Aug 12, 2017 at 22:02
  • Thanks!! That did it Commented Aug 12, 2017 at 22:03
  • 2
    The first time through your loop x will be 21, the second time 22 and so forth. When you try to access number[x], you're really accessing number[21]. As number isn't long enough to have an index 21, you're getting this error. Commented Aug 12, 2017 at 22:03

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.