Don't understand the error message : Invalid syntax in for statement
Question:
I am writing a very simple program to output the 0-10 in numbers using a for loop. However it comes up with a syntax error when I click run, highlighting in red the “=” in the 8th line. I don’t understand why it is wrong? I am using python 3.5.2 in idle mode.
def numbers():
print ("This program will count to ten, just you wait...")
import time
time.sleep(1)
print ("nnLiterally wait I just need to remember base 10 because I
only work in binary!")
time.sleep(4)
int(counter) = 0
for counter <**=** 9:
print ("n" + counter)
counter = counter + 1
print ("nnAnd there you go. Told you I could do it. Goodbye :) ")
time.sleep(2)
exit()
numbers()
Answers:
This is wrong syntax for for
. As per docs:
The for statement is used to iterate over the elements of a sequence (such as a string, tuple or list) or other iterable object.
which is not your case, or you can use range()
to create the sequence:
for i in range(1, 10):
yet it’s artificial workaround and while it will work, is not what you should be doing really.
You should use while
instead. Docs say:
The while statement is used for repeated execution as long as an expression is true
while counter <= 9:
Try it like this:
def numbers():
print ("This program will count to ten, just you wait...")
import time
time.sleep(1)
print ("nnLiterally wait I just need to remember base 10 because I only work in binary!")
time.sleep(4)
for i in range(1, 10): #11 if you want 10 to be printed
print i
print ("nnAnd there you go. Told you I could do it. Goodbye :) ")
time.sleep(2)
A couple of points. The excerpt:
int(counter) = 0
for counter <**=** 9:
print ("n" + counter)
counter = counter + 1
has multiple errors.
int(counter) = 0
is not valid python syntax.
for counter <**=** 9
is not a valid for
statement.
- The lines
print ("n" + counter)
and counter = counter + 1
are lacking proper indentation.
Replace those four lines with
for counter in range(10):
print(counter)
I am writing a very simple program to output the 0-10 in numbers using a for loop. However it comes up with a syntax error when I click run, highlighting in red the “=” in the 8th line. I don’t understand why it is wrong? I am using python 3.5.2 in idle mode.
def numbers():
print ("This program will count to ten, just you wait...")
import time
time.sleep(1)
print ("nnLiterally wait I just need to remember base 10 because I
only work in binary!")
time.sleep(4)
int(counter) = 0
for counter <**=** 9:
print ("n" + counter)
counter = counter + 1
print ("nnAnd there you go. Told you I could do it. Goodbye :) ")
time.sleep(2)
exit()
numbers()
This is wrong syntax for for
. As per docs:
The for statement is used to iterate over the elements of a sequence (such as a string, tuple or list) or other iterable object.
which is not your case, or you can use range()
to create the sequence:
for i in range(1, 10):
yet it’s artificial workaround and while it will work, is not what you should be doing really.
You should use while
instead. Docs say:
The while statement is used for repeated execution as long as an expression is true
while counter <= 9:
Try it like this:
def numbers():
print ("This program will count to ten, just you wait...")
import time
time.sleep(1)
print ("nnLiterally wait I just need to remember base 10 because I only work in binary!")
time.sleep(4)
for i in range(1, 10): #11 if you want 10 to be printed
print i
print ("nnAnd there you go. Told you I could do it. Goodbye :) ")
time.sleep(2)
A couple of points. The excerpt:
int(counter) = 0
for counter <**=** 9:
print ("n" + counter)
counter = counter + 1
has multiple errors.
int(counter) = 0
is not valid python syntax.for counter <**=** 9
is not a validfor
statement.- The lines
print ("n" + counter)
andcounter = counter + 1
are lacking proper indentation.
Replace those four lines with
for counter in range(10):
print(counter)