2015-09-25 49 views
0

我已经减少代码以仅显示我认为与以下错误相关的部分。这是在python 3.x中完成的,因为我正在教自己,所以我没有任何使用Python 2.x的经验。Python错误:无类型对象不可订阅

谢谢!

Traceback (most recent call last): 
File "rpcombat_refactored.py", line 306, in <module> shopping = buy_equipment() 
File "rpcombat_refactored.py", line 242, in buy_equipment 
print("You have a", join_with_and(profile['inventory']), "in your bag.") 
File "rpcombat_refactored.py", line 94, in join_with_and elif sequence < 1: 
TypeError: unorderable types: list() < int() 

代码:

def join_with_and(sequence): 
    if len(sequence) > 1: 
     last_item = sequence[-1] 
     sentence = ", ".join(sequence[:-1]) 
     sentence = sentence + " and " + last_item 
    elif sequence < 1: 
     sentence = "whole lot of nothing" 
    else: 
     sentence = sequence[0] 
    return sentence 

def buy_equipment(): 

    # Omitting item list 
    # Omitting purchase prompt 

    # If the item is in stock and the player has enough gold, buy it 
    if purchase in stock: 
     if stock[purchase][0] <= profile['gold']: 
      test_phrase = profile['Name'] + " buys themself some equipment" 
      print(fix_gender(profile['Gender'],test_phrase)) 
      print("You buy a", purchase, "for", stock[purchase][0], "gold pieces.") 
      profile['gold'] -= stock[purchase][0] 
      profile['inventory'].append(purchase) 
      print("You have a", join_with_and(profile['inventory']), "in your bag.") 
      print("You have", profile['gold'], "left.") 
     else: 
      print("You don't have enough gold to buy that.") 
    elif purchase == 'done' or purchase == "": 
     return profile['inventory'] == [] and profile['gold'] > 10 
    else: 
     print("We don't have", purchase, "in stock.") 
    return purchase 
+1

您需要a)将代码缩减为重现错误的基本要素,并且b)确保它正确缩进。在粘贴后选择代码缩进它后,使用编辑器工具栏上的“{}”按钮。你的'generate_rpc()'函数返回'None',因为在任何地方都没有显式的'return'语句,这是最可能的原因。但是,如果没有缩进,我不能确定。 –

+0

显然'profile is None',但如果你想让人们告诉你为什么,你必须将代码降低到[mcve];通过数百行代码挖掘几乎没有吸引力! – jonrsharpe

回答

1

generate_rpc方法不返回的个人资料。它什么都不返回。所以profile是None。

+0

谢谢Brian/Martijn/jonrsharpe,你是对的,这是造成所述错误的问题。如果我可能会问另一个项目,现在当我完成购物时,该程序正在吐出以下错误。回溯(最近最后调用): 文件 “rpcombat_refactored.py”,线307,在 手提包= join_with_and(配置文件[ '库存']) 文件 “rpcombat_refactored.py”,线94,在join_with_and elif的序列< 1: TypeError:无法订购的类型:list() masterj1109

+0

@ masterj1109“sequence”显然是一个列表list,而Python 3.x不允许你比较不同的类型(它可以在Python 2中工作.x,尽管可能仍然不符合你期望的行为)。请[橡皮鸭](https://en.wikipedia.org/wiki/Rubber_duck_debugging)。 – jonrsharpe

+0

@jonrsharpe:谢谢你的帮助!我不得不查阅橡皮鸭的参考资料,但它确实有帮助。这本书(我注意到很多让它过去重新审视的错误)忘记了包含长度函数是if语句的第二个条件。它应该是如果“len(sequence)<1”... – masterj1109

相关问题