UnboundLocalError:赋值前引用的局部变量“”userScore“”
UnboundLocalError:赋值前引用的局部变量“”comScore“”
我已经尝试将它们声明为全局变量,并将它们作为参数传递(有效,但每次都会重置分数)。下面是我的代码:
userScore = 0
comScore = 0
options = [[comRock, 'Rock'], [comPaper, 'Paper'], [comScissors, 'Scissors']]
# def start():
# startFrame.pack_forget()
# visualFrame.pack()
# controlFrame.pack()
def play(img, userIn):
comIn = random.choice(options)
Label(visualFrame, image=img, borderwidth=0, highlightthickness=0).grid(row=0, column=0)
Label(visualFrame, image=comIn[0], borderwidth=0, highlightthickness=0).grid(row=0, column=2)
Label(visualFrame, image=vsImg, borderwidth=0, highlightthickness=0).grid(row=0, column=1)
if userIn == comIn[1]:
Label(visualFrame, image=tieImg, borderwidth=0, highlightthickness=0).grid(row=0, column=0, columnspan=3)
elif (userIn == "Rock" and comIn[1] == "Scissors") or (userIn == "Scissors" and comIn[1] == "Paper") \
or (userIn == "Paper" and comIn[1] == "Rock"):
Label(visualFrame, image=wonImg, borderwidth=0, highlightthickness=0).grid(row=0, column=0, columnspan=3)
userScore += 1
else:
Label(visualFrame, image=lostImg, borderwidth=0, highlightthickness=0).grid(row=0, column=0, columnspan=3)
comScore += 1
Label(visualFrame, text=str(userScore) + ':' + str(comScore)).place(x=295, y=205)发布于 2021-08-10 01:49:20
紧跟在函数定义之后声明userScore global
def play(img, userIn):
global userScore
...https://stackoverflow.com/questions/68720103
复制相似问题