Python Forum

Full Version: Error in a "for" loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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]))
check your line 7
Thank you Buran. You have good eyes, but not me.
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.