:テール
def optimize(current_price = 0.1, last_profit = 0.0):
current_profit = profit(current_price)
if (last_profit > current_profit) and (current_profit > 0.0):
return {'best_price': current_price - 0.1, 'best_profit': last_profit}
# print({'best_price': current_price - 0.1, 'best_profit': last_profit})
else:
optimize(current_price + 0.1, current_profit)
def best_price():
optimized = optimize() # optimize() should return a dict,
# allowing optimized['best_price']
# and optimized['best_profit'] to be called
print("Pricing the tickets at ${0} will produce the greatest profit, ${1}.".format(optimized['best_price'], optimized['best_profit']))
機能は、それが何を返すのに失敗したことを除いて正常に動作します。最初のif
ステートメントが呼び出されないと言っているわけではありません(実際には、印刷ラインのコメントを外すと正しい結果が出力されます)が、returnステートメントは辞書を返さないことを意味します。
optimized['best_price']
を'NoneType' object is not subscriptable
として呼び出しようとすると、TypeError
になります。
私は今このエラーに取り組んでおり、自分自身を動作させたり、オンラインで何かを見つけることはできません。この時点で、解決策を知りたいのは私の問題です。何か案は?ありがとう!
は、なぜあなたは 'if'の各選択肢に' return'を持っていませんか? –