Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于文本的游戏资源清册列表(Python)_Python - Fatal编程技术网

基于文本的游戏资源清册列表(Python)

基于文本的游戏资源清册列表(Python),python,Python,所以我是python新手,我正在制作一个基于文本的游戏。我创建了一个库存清单,如果玩家不止一次捡到一个物品,第二次它应该能够给出一个消息,说他们已经有了这个物品。我让它在某种程度上工作,项目不会超过一个以上的值,但它不会打印消息。请帮忙 elif decision == "use H on comb": global inventory if inventory.count("comb")>1: print (

所以我是python新手,我正在制作一个基于文本的游戏。我创建了一个库存清单,如果玩家不止一次捡到一个物品,第二次它应该能够给出一个消息,说他们已经有了这个物品。我让它在某种程度上工作,项目不会超过一个以上的值,但它不会打印消息。请帮忙

elif decision == "use H on comb":
            global inventory
            if inventory.count("comb")>1:
                print ("You already got this item.")
                print ("")
                print ("Inventory: " + str(inventory))
            if inventory.count("comb")<1:
                print ("(pick up comb)")
                print ("You went over to the table and picked up the comb,")
                print ("it's been added to your inventory.")
                add_to_inventory("comb")
                print("")
                print ("Inventory: " + str(inventory))
            game()
elif decision==“在梳子上使用H”:
全球库存
如果库存盘点(“梳”)>1:
打印(“您已获得此项目。”)
打印(“”)
打印(“库存:+str(库存))

如果inventory.count(“comb”)只需使用
in
操作符测试成员资格

if "comb" in inventory:
   print("I have found the comb already...")
else:
   print("Nope not here")
但关于你的代码失败的原因是什么

inventory.count('comb') == 1 
# which fails inventory.count('comb') > 1 test
# but also fails inventory.count('comb') < 1 test so its not re added
inventory.count('comb')==1
#未通过库存盘点('comb')>1测试
#但也未能通过inventory.count('comb')<1测试,因此不会重新添加

您可以通过打印
inventory.count('comb')
的值来轻松解决这个问题,这是一种为初学者调试程序的有用方法。。。基本上,当某些内容无法正常工作时,请尝试打印它,很可能变量与您认为的不同…

只需使用
in
操作符测试成员身份即可

if "comb" in inventory:
   print("I have found the comb already...")
else:
   print("Nope not here")
但关于你的代码失败的原因是什么

inventory.count('comb') == 1 
# which fails inventory.count('comb') > 1 test
# but also fails inventory.count('comb') < 1 test so its not re added
inventory.count('comb')==1
#未通过库存盘点('comb')>1测试
#但也未能通过inventory.count('comb')<1测试,因此不会重新添加

您可以通过打印
inventory.count('comb')
的值来轻松解决这个问题,这是一种为初学者调试程序的有用方法。。。基本上,当某些东西不能正常工作时,请尝试打印它,很可能变量不是您认为的那样…

也许可以做更多的结构化工作,避免使用全局
库存
。js下面是一个基本想法:

def game():
     inventory = []
     # simulate picking up items( replace this loop with your custom logic )
     while True:
         item = raw_input('pick up something')
         if item in inventory: # use in operator to check membership
             print ("you already have got this")
             print (" ".join(inventory))
         else:
             print ("pick up the item")
             print ("its been added to inventory")
             inventory.append(item)
             print (" ".join(inventory))

也许可以多做一点结构化工作,避免使用全局
库存
。jsut下面是一个基本想法:

def game():
     inventory = []
     # simulate picking up items( replace this loop with your custom logic )
     while True:
         item = raw_input('pick up something')
         if item in inventory: # use in operator to check membership
             print ("you already have got this")
             print (" ".join(inventory))
         else:
             print ("pick up the item")
             print ("its been added to inventory")
             inventory.append(item)
             print (" ".join(inventory))