奇怪的 Python 语法错误
Strange Python syntax error
我正在创建一个程序,看起来它正在编译某些东西(实际上不是),但我遇到了一个奇怪的 invalid syntax
错误。我有以下代码:
import random
from time import sleep
files = ["main.cpp", "include.h", "connect.c", "mainProgram.java", "start.py", "lfm.java"]
verbs = ["Compiling ", "Checking ", "Debugging ", "Building "]
errors = ["stack_overflow", "divide_by_zero", "str_int_error", "syntax_error", "math_error"]
lineNums = ["4", "24", "13", "73", "48", "33", "172", "129", "145", "206"]
sleepFor = ["1", "3", "12", "15", "30", "43", "62"]
error = "Error: "
onLine = " on line "
minute = " (This may take a minute...)"
def random():
num = random.randint(0, 10)
return num
while True:
num = random()
if (num > 7):
print(random.choice(verbs) + random.choice(files) + minute)
sleep(random.choice(sleepFor))
elif (num == 0):
print(error + random.choice(errors) + onLine + random.choice(lineNums)
sleep(random.choice(sleepFor))
else:
print(random.choice(verbs) + random.choice(files))
sleep(random.choice(sleepFor))
出于某种原因,它在代码的以下部分(我调用 sleep
函数的地方)给我一个错误
elif (num == 0):
print(error + random.choice(errors) + onLine + random.choice(lineNums)
sleep(random.choice(sleepFor))
我的代码中没有发现任何语法错误。缩进有问题吗?我已经缩进了,所以我不这么认为。
此行缺少右括号:
print(error + random.choice(errors) + onLin+random.choice(lineNums)
更正为
print(error + random.choice(errors) + onLin+random.choice(lineNums))
您还没有关闭线路;
print(error + random.choice(errors) + onLin+random.choice(lineNums)
我建议您使用具有语法高亮显示功能的编辑器,这样可以显着提高速度;)
我假设你的意思是:
print(error + random.choice(errors) + onLin+random.choice(lineNums))
to_test=open("my_program.py")
program=to_test.read()
print(program.count("("))
print(program.count(")"))
21
20
我正在创建一个程序,看起来它正在编译某些东西(实际上不是),但我遇到了一个奇怪的 invalid syntax
错误。我有以下代码:
import random
from time import sleep
files = ["main.cpp", "include.h", "connect.c", "mainProgram.java", "start.py", "lfm.java"]
verbs = ["Compiling ", "Checking ", "Debugging ", "Building "]
errors = ["stack_overflow", "divide_by_zero", "str_int_error", "syntax_error", "math_error"]
lineNums = ["4", "24", "13", "73", "48", "33", "172", "129", "145", "206"]
sleepFor = ["1", "3", "12", "15", "30", "43", "62"]
error = "Error: "
onLine = " on line "
minute = " (This may take a minute...)"
def random():
num = random.randint(0, 10)
return num
while True:
num = random()
if (num > 7):
print(random.choice(verbs) + random.choice(files) + minute)
sleep(random.choice(sleepFor))
elif (num == 0):
print(error + random.choice(errors) + onLine + random.choice(lineNums)
sleep(random.choice(sleepFor))
else:
print(random.choice(verbs) + random.choice(files))
sleep(random.choice(sleepFor))
出于某种原因,它在代码的以下部分(我调用 sleep
函数的地方)给我一个错误
elif (num == 0):
print(error + random.choice(errors) + onLine + random.choice(lineNums)
sleep(random.choice(sleepFor))
我的代码中没有发现任何语法错误。缩进有问题吗?我已经缩进了,所以我不这么认为。
此行缺少右括号:
print(error + random.choice(errors) + onLin+random.choice(lineNums)
更正为
print(error + random.choice(errors) + onLin+random.choice(lineNums))
您还没有关闭线路;
print(error + random.choice(errors) + onLin+random.choice(lineNums)
我建议您使用具有语法高亮显示功能的编辑器,这样可以显着提高速度;)
我假设你的意思是:
print(error + random.choice(errors) + onLin+random.choice(lineNums))
to_test=open("my_program.py")
program=to_test.read()
print(program.count("("))
print(program.count(")"))
21
20