你好,我熟悉python,在c++之后我断然不明白使用全局变量的原理。如果一个位于def中的变量指定了全局,那么它的值是否可以在这个函数之外使用,我肯定不能。也许只是因为我是一个初学者,而显而易见的事情对我来说仍然太复杂了。
def check():
global urokb_in
urokb_in = 1
with open('users.txt', 'r') as f:
line = f.readline()
f.close()
with open('progress.txt', 'r') as k:
while True:
global uroka_in
lines = k.readline()
if line == lines.rstrip():
uroka_in = int(k.readline().rstrip())
urokb_in = uroka_in + 1
uroka_str = str(uroka_in)
urokb_str = str(urokb_in)
lines.replace(uroka_str, urokb_str)
k.close()
break
if not lines:
k.close()
urokb_in = 1
with open('progress.txt', 'a') as j:
j.write(line)
j.write('\n')
j.write(str(urokb_in))
j.write('\n')
j.close()
break
if urokb_in == 1:
educate__scr = PhotoImage(file='e_scr_1.png')
educate_label = Label(root, image=educate__scr)
educate_label.place(x=0, y=0)
更有经验的编码人员告诉我如何解决它。SyntaxError: name 'urokb_in' 在全局声明之前使用