SyntaxError:变量在全局声明[closed]之前分配给它

问题描述 投票:-1回答:3
我为自己编写了一个脚本,可以为自己的动作创建自动化。没用...它说

File "genstr.py", line 20 global lastnum ^ SyntaxError: name 'lastnum' is assigned to before global declaration

我认为这应该起作用。

我尝试了什么:以前它说的是一样的,但是在第23行和大约print('OK')上,直到我没有将其删除为止。现在说到elif。您能帮我解决我的错误吗?代码本身:

def genletters(): # --------------------------------------------- # main variables lastnum = -1 beginbool = input('Is this the first time you create the document? y/n') # --------------------------------------------- # checking for user answer if beginbool == y: print('OK') elif beginbool == n: global lastnum # < Error line. print('OK') lastnum = int(input('Then what it was?') elif beginbool != y and beginbool != n: input('bruh?') import sys sys.exit() # --------------------------------------------- typestr = input('What is your string?') # --------------------------------------------- # additional vars additional = 1 strlen = len(typestr) listsep = 0 # --------------------------------------------- # MAIN LOOP while strlen != 0: print('vidmem[', numerical + additional, '] = ', typestr[:listsep], ';\n', end='', sep='') additional = additional + 1 print('vidmem[', numerical + additional, '] = 0x04;\n', end='', sep='') additional = additional + 1 listsep = listsep + 1 strlen = strlen - 1
[如果可以的话,请告诉我我必须解决的问题和错误。更多信息:我使用Ubuntu Terminal的python3 genstr.py启动应用程序。    
python syntax syntax-error gedit
3个回答
1
投票
我认为您缺少括号

0
投票
[您错过了一个括号,并在字符串周围倒了逗号。更改这两行:

0
投票
我为第一行添加了一个括句,并删除了global lastnum行,现在可以使用。
© www.soinside.com 2019 - 2024. All rights reserved.