我已经减少代码以仅显示我认为与以下错误相关的部分。这是在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
您需要a)将代码缩减为重现错误的基本要素,并且b)确保它正确缩进。在粘贴后选择代码缩进它后,使用编辑器工具栏上的“{}”按钮。你的'generate_rpc()'函数返回'None',因为在任何地方都没有显式的'return'语句,这是最可能的原因。但是,如果没有缩进,我不能确定。 –
显然'profile is None',但如果你想让人们告诉你为什么,你必须将代码降低到[mcve];通过数百行代码挖掘几乎没有吸引力! – jonrsharpe