奇怪的Python语法错误

时间:2015-10-01 13:54:21

标签: 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))

我的代码中没有看到任何语法错误。缩进有问题吗?我把它缩进了,所以我不这么认为。

3 个答案:

答案 0 :(得分:2)

这一行缺少右括号:

print(error + random.choice(errors) + onLin+random.choice(lineNums)

纠正

print(error + random.choice(errors) + onLin+random.choice(lineNums))

答案 1 :(得分:0)

您尚未关闭该行;

>>=

我建议你使用一个具有语法高亮功能的编辑器,这会使这个显然变得非常快;)

我认为您的意思如下:

print(error + random.choice(errors) + onLin+random.choice(lineNums)

答案 2 :(得分:0)

to_test=open("my_program.py")
program=to_test.read()

print(program.count("("))
print(program.count(")"))

21

20