-1

when i am working on below code,python returns

for(temp in range(len(history_list))):
                                 ^
SyntaxError: invalid syntax"

i really can't figure out what cause the error ,if you have any idea please leave a comment,thank you.

    with open('history.csv', 'r',newline='') as file:
        history_list = csv.reader(csvfile)
    for(temp in range(len(history_list))):
        if(keyword==history_list[temp][0]):
            #do something
            break
930727fre
  • 41
  • 2
  • 6
  • 1
    this is not c++, XD you don't need brackets for ```for loops``` – nasc Mar 30 '21 at 04:26
  • 1
    Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [ask] from the [tour]. "Teach me this basic language feature" is off-topic for Stack Overflow. Stack Overflow is not intended to replace existing tutorials and documentation. – TigerhawkT3 Mar 30 '21 at 04:28
  • 1) For loop should be within the with block and 2) history_list will be an iterator so you can't index into as you are trying within the for loop i.e. history_list[temp] won't work. – DarrylG Mar 30 '21 at 04:31

1 Answers1

0

No need for ( after for.

Rewrite as

for temp in range(len(history_list)):
    blah blah

On a more pythonic note, reconsider writing the loop as:

for item in history_list:
    blah blah
sureshvv
  • 4,234
  • 1
  • 26
  • 32
  • Have added a recommendation to make code more pythonic. – sureshvv Mar 30 '21 at 04:29
  • @930727fre - Ideally you should proofread your code for that kind of mistake before posting a question, but you can also delete such questions, which I'd recommend here. – TigerhawkT3 Mar 30 '21 at 04:32
  • Use a tool like PyFlakes to syntax check your code. It integrates into most code editors, making it easy to use. – sureshvv Mar 30 '21 at 04:37