import random
n = str(["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC",])
for index in range(n):
Asked
Active
Viewed 4,657 times
-4

Matiiss
- 5,970
- 2
- 12
- 29

user17206558
- 1
- 1
- 1
-
6The argument to `range()` must be an integer, but you passed a string instead. – John Gordon Oct 21 '21 at 01:02
-
4please provide a [mre] and explain what you want to achieve – Matiiss Oct 21 '21 at 01:11
1 Answers
0
In this line:
for index in range(n):
range()
is a function in python that the argument is an integer, but you pass n
, which is a string.
If you want to loop over the strings in that list, try:
import random
n = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC",]
for index in n:
print(index) # print the string in the list
If you want the index of the element in the array, try:
import random
n = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC",]
for index, string in enumerate(n):
print(index) # print the index of the string in the list
print(string) # print the string in the list

justANewb stands with Ukraine
- 4,535
- 3
- 14
- 39
-
2I am pretty sure that index means the index not the string but I may be wrong on OPs intentions – Matiiss Oct 21 '21 at 01:20
-
@Matiiss Thanks for your suggestion! Due to the lack of content, we can do nothing better but guessing ;) – justANewb stands with Ukraine Oct 21 '21 at 01:30
-
@justANewbie, actually, if a question is unclear, you should ask the OP to clarify the question instead of guessing. Otherwise we might get a lot of different responses that answer different things. And that's not how SO works. You can leave a comment for the OP, or use the "flag" option under the post. This will lead to higher quality questions and answers on the site! – wovano Oct 21 '21 at 11:53
-
@wovano actually IMO his question is clear. He's getting an error, and I fix it. He just didn't tell what his intention was, and that's okay. – justANewb stands with Ukraine Oct 21 '21 at 12:30
-
@justANewbie, but SO is not a personal support forum. It's a Q&A database with (hopefully, high-quality) programming questions and answers. A question like this will likely not be useful for anyone else except the person who asked it. If it would have been rewritten into something more general, it could benefit more persons. And that's the intention of the website. See [this meta question](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) (and especially, its answer). Or [this one](https://meta.stackoverflow.com/questions/261592/) – wovano Oct 21 '21 at 12:34