2017-10-27 6 views
0

私は自分のゲームで作成しようとした棚卸のアイテムを削除して追加したいのですが、エラーが表示され続けます。これは私のコードです:テキストベースゲームで自分の在庫が間違っていることがわかりません

inventory={} 
def add_to_inventory(): 
    inventory.append() 

elif choice == "use h on razor": 
     print ("(pick up razor)") 
     if "razor" in inventory: 
      print ("You already got this item.") 
      print ("") 
      print ("Inventory: " + str(inventory)) 
     if "razor" not in inventory: 
      print ("You walked over and picked up your razor blade.") 
      print ("It's been added to your inventory.") 
      add_to_inventory("razor") 
      print("") 
      print ("Inventory: " + str(inventory)) 
     game() 

これは私が私のゲームを実行したとき、私は受信エラーです:

(かみそりを拾う) あなたは以上歩いて、あなたのカミソリの刃を拾いました。 あなたの在庫に追加されました。 add_to_inventoryは、関数定義のパラメータはありませんので、

Traceback (most recent call last): 
    File "C:\Users\Owner\AppData\Local\Programs\Python\Python36-32\inferno_junction_2017-10-27.py", line 337, in <module> 
    instructions_part_1() 
    File "C:\Users\Owner\AppData\Local\Programs\Python\Python36-32\inferno_junction_2017-10-27.py", line 336, in instructions_part_1 
    try_1() 
    File "C:\Users\Owner\AppData\Local\Programs\Python\Python36-32\inferno_junction_2017-10-27.py", line 310, in try_1 
    instructions_part_2() 
    File "C:\Users\Owner\AppData\Local\Programs\Python\Python36-32\inferno_junction_2017-10-27.py", line 304, in instructions_part_2 
    try_2() 
    File "C:\Users\Owner\AppData\Local\Programs\Python\Python36-32\inferno_junction_2017-10-27.py", line 286, in try_2 
    instructions_part_3() 
    File "C:\Users\Owner\AppData\Local\Programs\Python\Python36-32\inferno_junction_2017-10-27.py", line 280, in instructions_part_3 
    try_3() 
    File "C:\Users\Owner\AppData\Local\Programs\Python\Python36-32\inferno_junction_2017-10-27.py", line 266, in try_3 
    instructions_part_4() 
    File "C:\Users\Owner\AppData\Local\Programs\Python\Python36-32\inferno_junction_2017-10-27.py", line 259, in instructions_part_4 
    main() 
    File "C:\Users\Owner\AppData\Local\Programs\Python\Python36-32\inferno_junction_2017-10-27.py", line 231, in main 
    start() 
    File "C:\Users\Owner\AppData\Local\Programs\Python\Python36-32\inferno_junction_2017-10-27.py", line 219, in start 
    game() 
    File "C:\Users\Owner\AppData\Local\Programs\Python\Python36-32\inferno_junction_2017-10-27.py", line 57, in game 
    game() 
    File "C:\Users\Owner\AppData\Local\Programs\Python\Python36-32\inferno_junction_2017-10-27.py", line 155, in game 
    add_to_inventory("razor") 
TypeError: add_to_inventory() takes 0 positional arguments but 1 was given 
+0

は多分問題が –

+0

skdbelsldjdwlslkdneñsldjdnラインにほんの少し金曜日の冗談:)にStackOverflowに –

+1

ようこそ申し訳ありませんです。ヘルプドキュメントの投稿ガイドラインを読み、それに従ってください。 [最小、完全で検証可能な例](http://stackoverflow.com/help/mcve)がここに適用されます。 MCVEコードを投稿して問題を正確に記述するまでは、効果的にお手伝いすることはできません。 投稿したコードをテキストファイルに貼り付け、説明した問題を再現できるはずです。 – Prune

答えて

0

あなたのエラーが発生しているが、あなたはそれへの引数として渡す'razor'をしようとしています。これは、TypeError: add_to_inventory() takes 0 positional arguments but 1 was givenの意味です。

また、appendリストを使用していますが、インベントリを辞書にしています。

ここでは、希望する機能に応じて、辞書用とリスト用の2つのオプションを使用できます。

#DICTIONARY INVENTORY 

from collections import defaultdict 
inventory = defaultdict(int) 

def add_to_inventory(item, amount): 
    inventory[item] += amount 

#LIST INVENTORY 

inventory = [] 
def add_to_inventory(item): 
    inventory.append(item) 
+1

うわー、ありがとうございます! 2番目のオプションは私の問題を解決するように見えました! – John

+0

'if' /' else'パターンは、標準ライブラリによって扱われる共通のパターンです。 'inventory = collections.defaultdict(int)'の使用を検討してください。 https://docs.python.org/3/library/collections.html#collections.defaultdict –

関連する問題