首页 > 解决方案 > 类型错误:+ 的不支持的操作数类型:“int”和“str”在之前执行良好的程序中

问题描述

在这样做的同时

year = 2019
tariq1 = year+'-01-01'
tariq2 = year+'-12-31'
while year > 2015:
    for stock in string:
        max=quandl.get(stock, start_date=tariq1, end_date=tariq2)
        max
    year = year - 1

在行中出现错误

tariq =year+'-01-01'

_---------------------------------------------------- ------------------------- TypeError Traceback (最近一次调用最后一次) in ()

     1 year = 2019
----> 2 tariq1 = year+'-01-01'
      3 tariq2 = year+'-12-31'
      4 while year > 2015:
      5     for stock in string:_

类型错误:+ 不支持的操作数类型:“int”和“str”

其中 quandl.get 将数据帧返回到最大值。我在另一个代码中也遇到了同样的错误。我之前在多次执行相同的代码时没有遇到过这种情况。现在才得到这个。欢迎任何帮助。谢谢

标签: pythonstringintoperands

解决方案


您需要year在连接之前转换为字符串。

year = 2019
tariq1 = str(year)+'-01-01'
tariq2 = str(year)+'-12-31'
while year > 2015:
    for stock in string:
        max=quandl.get(stock, start_date=tariq1, end_date=tariq2)
        max
    year = year - 1

附带说明一下,您可能还想更新循环内部的值,tariq1tariq2不是在循环之前:

year = 2019
while year > 2015:
    tariq1 = str(year)+'-01-01'
    tariq2 = str(year)+'-12-31'
    for stock in string:
        max=quandl.get(stock, start_date=tariq1, end_date=tariq2)
        max
    year = year - 1

推荐阅读