Python Forum
Error in a "for" loop - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Error in a "for" loop (/thread-5979.html)



Error in a "for" loop - sylas - Oct-31-2017

Hi all! It seems easy but it is not. Thanks for your help.
print("quick_average.py")
text = "13 16 19 23"
x = text.split()
print(x)  # x is a list of sytrigs
print(x[0])  # print first item
print(float(x[0]))  # convert string to number
print("convert the whole list" =
for i in range(4):  # syntax error
# for i in text.split():   #syntax error
    print(x[i])
print(float(x[i]))
Error:
/usr/bin/python3.5 /home/sylvain/PycharmProjects/Py2/quick_average.py   File "/home/sylvain/PycharmProjects/Py2/quick_average.py", line 8     for i in range(4):       ^ SyntaxError: invalid syntax Process finished with exit code 1



tagged code:
print("quick_average.py")
text = "13 16 19 23"
x = text.split()
print(x)  # x is a list of sytrigs
print(x[0])  # print first item
print(float(x[0]))  # convert string to number
print("convert the whole list" =
for i in range(4):  # syntax error
# for i in text.split():   #syntax error
    print(x[i])
print(float(x[i]))



RE: Error in a "for" loop - buran - Oct-31-2017

check your line 7


RE: Error in a "for" loop - sylas - Oct-31-2017

Thank you Buran. You have good eyes, but not me.


RE: Error in a "for" loop - nilamo - Oct-31-2017

Sometimes, with Syntax errors, you need to check the line before where it says the error is.  And that's because of things like this, where a closing parenthesis is expected, but something else is found instead.