【问题标题】:How could i fix the 'local variable 'prev_time' defined in enclosing scope on line 6 referenced before assignment" error我如何修复在赋值前引用的第 6 行封闭范围中定义的“局部变量‘prev_time’”错误
【发布时间】:2020-11-03 23:42:59
【问题描述】:

我正在使用 Python 的“时间”库,现在我正在努力获取要更新的日期变量。现在我的代码是这样的

def getTime():
  named_tuple = time.localtime()
  time_string = time.strftime("%m/%d/%Y", named_tuple)
  prev_time = time_string

getTime()

if prev_time != time_string:
  getTime()

在“prev_time != time string:”上,我不断收到错误“'local variable 'prev_time' defined in enclosure scope on line 6 in referenced before assignment”我对 python 比较陌生,所以任何帮助将不胜感激!提前致谢!

【问题讨论】:

    标签: python datetime error-handling shared-libraries


    【解决方案1】:

    尝试使用全局范围。

    def getTime():
      global prev_time,time_string
      named_tuple = time.localtime()
      time_string = time.strftime("%m/%d/%Y", named_tuple)
      prev_time = time_string
    
    getTime()
    
    if prev_time != time_string:
      getTime()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多