【问题标题】:TypeError: unorderable types: str() <= int()TypeError:不可排序的类型:str() <= int()
【发布时间】:2016-08-22 09:51:34
【问题描述】:

我正在尝试获取一个列表,share_list,然后一个一个地循环遍历该列表并生成一个适合结果的输出。我有两个问题:我不知道如何使用for 循环在列表中循环,并且出现此错误:

Traceback (most recent call last):
  File "C:\Users\Andrew\Documents\Python Projects\DataAnalytics\algorithm.py", line 9, in <module>
    if check_pb_ratio.get_price_book() <= 1:
TypeError: unorderable types: str() <= int()
from yahoo_finance import Share

share_list = ['AAPL', 'GEVO', 'PTX']


for ticker in share_list:
    check_pb_ratio = Share(share_list[0])

    if check_pb_ratio.get_price_book() <= 1:
        print(str(check_pb_ratio.get_price_book()))
    else:
        print("P/B Ratio is too high.")

【问题讨论】:

  • 看来check_pb_ratio.get_price_book() 是一个字符串。尝试在if 之前打印。
  • share_list[0] -> ticker, check_pb_ratio.get_price_book() -> float(check_pb_ratio.get_price_book())。考虑使用 google 和/或 python 教程。

标签: python typeerror python-3.5


【解决方案1】:

原因是函数check_pb_ratio.get_price_book()返回一个字符串,而不是一个int。 Python 不想知道 '1' 和 1 之间的相似性。
所以,修复它的方法是:在 check_pb_ratio.get_price_book() 周围添加 int()float()

【讨论】:

    【解决方案2】:

    感谢@Rawing

    这段代码现在可以工作了,我只需要修复一个 NoneType 问题。谢谢!

    from yahoo_finance import Share
    
    share_list = ['AAPL', 'GEVO', 'PTX']
    
    
    for ticker in share_list:
        check_pb_ratio = Share(ticker)
    
        if float(check_pb_ratio.get_price_book()) <= 1:
            print(str(check_pb_ratio.get_price_book()))
        else:
            print("P/B Ratio is too high.")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-18
      • 1970-01-01
      • 2013-01-30
      • 1970-01-01
      • 2017-03-25
      • 1970-01-01
      相关资源
      最近更新 更多