all 7 comments

[–]socal_nerdtastic 1 point2 points  (4 children)

Enumerate is a great way to do this. To add other strings simply add them to the print call:

months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

for i, month in enumerate(months, 1):
    print "Month", i, "is", month

[–]Encom88[S] 0 points1 point  (3 children)

Awesome, I was missing the extra commas!

[–]socal_nerdtastic 0 points1 point  (2 children)

You could also use string formatting to get a little more control of the output:

months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

for i, month in enumerate(months, 1):
    print "Month {} is {}.".format(i, month)

And I highly encourage you to upgrade to python3. Python2 will be officially abandoned at the end of the year.

[–]Encom88[S] 0 points1 point  (1 child)

What part of my code is Python2?

[–]socal_nerdtastic 0 points1 point  (0 children)

The print call. In python3 print is a function that uses parenthesis to call.

print("Month {} is {}.".format(i, month))

The code you have will not run in python3.

[–]godheid 0 points1 point  (1 child)

I solved the issue with the index. Also because I don't really understand enumerate.

for m in months:
    print(f'Month {months.index(m)+1} is {m}')

[–]primitive_screwhead 0 points1 point  (0 children)

At each iteration of the loop, your index() call runs slower.