Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error in a "for" loop
#1
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]))
Reply
#2
check your line 7
Reply
#3
Thank you Buran. You have good eyes, but not me.
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Com Error with macro within for loop due to creating new workbook in the loop Lastwizzle 0 1,424 May-18-2019, 09:29 PM
Last Post: Lastwizzle

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020