不明白错误消息:for 语句中的语法无效
Don't understand the error message : Invalid syntax in for statement
我正在编写一个非常简单的程序,使用 for 循环输出 0-10 的数字。但是,当我单击 运行 时出现语法错误,以红色突出显示第 8 行中的“=”。我不明白为什么它是错误的?我在空闲模式下使用 python 3.5.2。
def numbers():
print ("This program will count to ten, just you wait...")
import time
time.sleep(1)
print ("\n\nLiterally 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 ("\n\nAnd there you go. Told you I could do it. Goodbye :) ")
time.sleep(2)
exit()
numbers()
for
的语法错误。根据 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.
这不是你的情况,或者你可以使用 range()
创建序列:
for i in range(1, 10):
然而这是人为的解决方法,虽然它会起作用,但并不是您真正应该做的。
您应该改用 while
。 Docs 说:
The while statement is used for repeated execution as long as an expression is true
while counter <= 9:
这样试试:
def numbers():
print ("This program will count to ten, just you wait...")
import time
time.sleep(1)
print ("\n\nLiterally 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 ("\n\nAnd there you go. Told you I could do it. Goodbye :) ")
time.sleep(2)
几点。节选:
int(counter) = 0
for counter <**=** 9:
print ("\n" + counter)
counter = counter + 1
有多个错误。
int(counter) = 0
是无效的 python 语法。
for counter <**=** 9
不是有效的 for
语句。
- 行
print ("\n" + counter)
和 counter = counter + 1
缺少正确的缩进。
将这四行替换为
for counter in range(10):
print(counter)
我正在编写一个非常简单的程序,使用 for 循环输出 0-10 的数字。但是,当我单击 运行 时出现语法错误,以红色突出显示第 8 行中的“=”。我不明白为什么它是错误的?我在空闲模式下使用 python 3.5.2。
def numbers():
print ("This program will count to ten, just you wait...")
import time
time.sleep(1)
print ("\n\nLiterally 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 ("\n\nAnd there you go. Told you I could do it. Goodbye :) ")
time.sleep(2)
exit()
numbers()
for
的语法错误。根据 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.
这不是你的情况,或者你可以使用 range()
创建序列:
for i in range(1, 10):
然而这是人为的解决方法,虽然它会起作用,但并不是您真正应该做的。
您应该改用 while
。 Docs 说:
The while statement is used for repeated execution as long as an expression is true
while counter <= 9:
这样试试:
def numbers():
print ("This program will count to ten, just you wait...")
import time
time.sleep(1)
print ("\n\nLiterally 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 ("\n\nAnd there you go. Told you I could do it. Goodbye :) ")
time.sleep(2)
几点。节选:
int(counter) = 0
for counter <**=** 9:
print ("\n" + counter)
counter = counter + 1
有多个错误。
int(counter) = 0
是无效的 python 语法。for counter <**=** 9
不是有效的for
语句。- 行
print ("\n" + counter)
和counter = counter + 1
缺少正确的缩进。
将这四行替换为
for counter in range(10):
print(counter)