如何修复未绑定的本地错误?

2024-06-30 08:43:35 发布

您现在位置:Python中文网/ 问答频道 /正文

错误 selfpoints+=卡点数 UnboundLocalError:赋值前引用了局部变量“selfpoints”

class userpoint_status:
     def __init__():
         selfpoints = 0
     def increase_points():
         self.points += cardPoints
         print("You have",selfpoints,"points")
         if selfpoints > 21:
                 lose_game() 

Tags: selfinitdefstatus错误pointsclass赋值
1条回答
网友
1楼 · 发布于 2024-06-30 08:43:35

在您的代码中有多处出现,您键入的是selfpoints,而不是self.points。你需要把点放在那里,这样它就变成了一个实例变量。假定cardPointslose_game是全局变量,您就不会再有任何错误了。你知道吗

相关问题 更多 >