【问题标题】:How do i fix the unbound local error?如何修复未绑定的本地错误?
【发布时间】:2017-12-09 19:39:21
【问题描述】:

错误 selfpoints += cardPoints 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() 

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    您的代码中有多次出现您键入selfpoints 而不是self.points。您需要将点放在那里,使其成为实例变量。鉴于cardPointslose_game 是全局变量,您不会再有任何错误。

    【讨论】:

      猜你喜欢
      • 2016-07-05
      • 2021-05-03
      • 2016-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多