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
启动应用程序。
global lastnum
行,现在可以使用。