我应该如何增加 python 代码中的分数变量?
How should i increase the score variable in my python code?
from turtle import Turtle
SCORE=0
class Scoreboard(Turtle):
def __init__(self):
super().__init__()
self.color("white")
self.up()
self.hideturtle()
self.goto(-10,290)
self.write(f"score : {SCORE} ",align="center",font=("Arial",16,"normal"))
def score_change(self):
SCORE+=1
self.__init__()'''
我是新手 python.I 我想在从 main.py 调用方法时增加 SCORE 变量,但我收到错误,例如在 [=17] 之前引用的局部变量 'SCORE' =] 知道有些帖子有这个错误,但我想找到这个代码的解决方案。
由于SCORE
是一个全局变量,请在函数的SCORE
前加上global
关键字。因此,当您执行 SCORE += 1
时,找不到 SCORE
,如 python 您需要明确告诉 global
访问 未封装的变量 在 class 中,它们被解释为 class 的静态成员,就像您的情况一样。
改变你的方法如下
def score_change(self):
global SCORE # add this line
SCORE += 1
self.__init__()
更正来源:
from turtle import Turtle
SCORE = 0
class Scoreboard(Turtle):
def __init__(self):
super().__init__()
self.color("white")
self.up()
self.hideturtle()
self.goto(-10,290)
self.write(f"score : {SCORE} ",align="center",font=("Arial",16,"normal"))
def score_change(self):
global SCORE
SCORE += 1
self.__init__()
if __name__ == '__main__':
src = Scoreboard()
src.score_change()
src.score_change()
src.score_change()
print(SCORE)
# Output:
3
我还建议,如果 SCORE 仅在记分板 Class 的上下文中使用,那么您应该如下所示将其设为静态成员,并像以前一样更改 def score_change()
方法。而访问SCORE
我们如何访问静态成员。喜欢 Scoreboard.SCORE
我的建议来源:
from turtle import Turtle
class Scoreboard(Turtle):
SCORE = 0
def __init__(self):
super().__init__()
self.color("white")
self.up()
self.hideturtle()
self.goto(-10,290)
self.write(f"score : {Scoreboard.SCORE} ",align="center",font=("Arial",16,"normal"))
def score_change(self):
Scoreboard.SCORE += 1
self.__init__()
if __name__ == '__main__':
src = Scoreboard()
src.score_change()
src.score_change()
src.score_change()
src.score_change()
src.score_change()
print(Scoreboard.SCORE)
from turtle import Turtle
SCORE=0
class Scoreboard(Turtle):
def __init__(self):
super().__init__()
self.color("white")
self.up()
self.hideturtle()
self.goto(-10,290)
self.write(f"score : {SCORE} ",align="center",font=("Arial",16,"normal"))
def score_change(self):
SCORE+=1
self.__init__()'''
我是新手 python.I 我想在从 main.py 调用方法时增加 SCORE 变量,但我收到错误,例如在 [=17] 之前引用的局部变量 'SCORE' =] 知道有些帖子有这个错误,但我想找到这个代码的解决方案。
由于SCORE
是一个全局变量,请在函数的SCORE
前加上global
关键字。因此,当您执行 SCORE += 1
时,找不到 SCORE
,如 python 您需要明确告诉 global
访问 未封装的变量 在 class 中,它们被解释为 class 的静态成员,就像您的情况一样。
改变你的方法如下
def score_change(self):
global SCORE # add this line
SCORE += 1
self.__init__()
更正来源:
from turtle import Turtle
SCORE = 0
class Scoreboard(Turtle):
def __init__(self):
super().__init__()
self.color("white")
self.up()
self.hideturtle()
self.goto(-10,290)
self.write(f"score : {SCORE} ",align="center",font=("Arial",16,"normal"))
def score_change(self):
global SCORE
SCORE += 1
self.__init__()
if __name__ == '__main__':
src = Scoreboard()
src.score_change()
src.score_change()
src.score_change()
print(SCORE)
# Output:
3
我还建议,如果 SCORE 仅在记分板 Class 的上下文中使用,那么您应该如下所示将其设为静态成员,并像以前一样更改 def score_change()
方法。而访问SCORE
我们如何访问静态成员。喜欢 Scoreboard.SCORE
我的建议来源:
from turtle import Turtle
class Scoreboard(Turtle):
SCORE = 0
def __init__(self):
super().__init__()
self.color("white")
self.up()
self.hideturtle()
self.goto(-10,290)
self.write(f"score : {Scoreboard.SCORE} ",align="center",font=("Arial",16,"normal"))
def score_change(self):
Scoreboard.SCORE += 1
self.__init__()
if __name__ == '__main__':
src = Scoreboard()
src.score_change()
src.score_change()
src.score_change()
src.score_change()
src.score_change()
print(Scoreboard.SCORE)