【问题标题】:when i run the program it throws error and in vs code it is giving "error:location.condition is not callable"当我运行程序时它会抛出错误,在 vs 代码中它给出“错误:location.condition 不可调用”
【发布时间】:2019-05-03 18:04:19
【问题描述】:

我正在尝试使用 python 构建一个助手。它一直显示错误 "location.condition 不可调用 pylint(not-callable)" & "location.forecast 不可调用 pylint(not-callable)"

    elif 'current weather in' in command:   
        reg_ex = re.search('current weather in (.*)', command)
        if reg_ex:
            city = reg_ex.group(1)
            weather = Weather(unit=Unit.CELSIUS)
            location = weather.lookup_by_location(city)
            condition = location.condition()
            TalkToMe('The Current weather in %s is %s.' 
            'The tempeture is %d.1 C degree' %(city, condition.text(), 
               (int(condition.temp))))

    elif 'weather forecast in' in command:
        reg_ex = re.search('weather forecast in (.*)', command)
        if reg_ex:
            city = reg_ex.group(1)
            weather = Weather()
            location = weather.lookup_by_location(city)
            forecasts = location.forecast()
            for i in range(0,3):
                TalkToMe("On %s will it %s."
                'The maximum temperture will be %d.1 C degree.'
                'The lowest temperature will be %d.1 C degrees.' % (forecasts[i].date(), forecasts[i].text(), (int(forecasts[i].high)), (int(forecasts[i].low))))

它应该告诉天气状况或天气预报

【问题讨论】:

  • condition 是属性而不是方法。试试location.condition 而不是location.condition()。预测也一样。此外,无需大写。
  • @glhr,确实,我已经编辑了答案中的大写字母。

标签: python visual-studio-code weather-api assistant


【解决方案1】:

https://pypi.org/project/weather-api/ 的示例显示:

weather = Weather(unit=Unit.CELSIUS)
location = weather.lookup_by_location('dublin')
condition = location.condition
print(condition.text)

但是,你正在做lookup.condition()。括号导致 Python “调用”lookup.condition,即要求它是可调用的。

请注意,pylint 是一个静态代码分析器。它会尝试提前警告您代码中的问题,以便您可以在实际运行程序之前修复它们。静态代码分析器并不总是正确的,但在这种情况下似乎是正确的。删除括号应该可以解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-16
    • 1970-01-01
    • 1970-01-01
    • 2022-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多