【问题标题】:Inventory help Python [closed]库存帮助Python [关闭]
【发布时间】:2017-10-19 13:24:22
【问题描述】:

因此,如果玩家的库存中可能/没有某些物品,我正在尝试安排我的文本基础游戏的游戏玩法。

 print ("You are back in your cell. You saw your bed, broken sink, grotty toilet, cut up jumpsuit")
    if "comb" and "razor" in inventory:
        print ("and the table with the empty bottle.")
    else "comb" not in inventory and "razor" in inventory:
        print ("and the table with the empty bottle and comb.")
    else "razor" not in inventory and "comb" in inventory:
        print ("and the table with the empty bottle and razor")

它告诉我这行代码有语法错误

else "comb" not in inventory and "razor" in inventory:

我似乎看不出我犯了什么错误,我是一个初学者,所以也许有另一种方法来满足我的需求。

【问题讨论】:

  • 我想你的意思是elif,而不是else

标签: python inventory text-based


【解决方案1】:

你快到了

else 只能这样工作

else:
    do something

所以你的代码会是这样的

 print ("You are back in your cell. You saw your bed, broken sink, grotty toilet, cut up jumpsuit")
    if "comb" and "razor" in inventory:
        print ("and the table with the empty bottle.")
    elif "comb" not in inventory and "razor" in inventory:
        print ("and the table with the empty bottle and comb.")
    elif "razor" not in inventory and "comb" in inventory:
        print ("and the table with the empty bottle and razor")

或者那个

 print ("You are back in your cell. You saw your bed, broken sink, grotty toilet, cut up jumpsuit")
    if "comb" and "razor" in inventory:
        print ("and the table with the empty bottle.")
    elif "comb" not in inventory and "razor" in inventory:
        print ("and the table with the empty bottle and comb.")
    else: #using the else here
        print ("and the table with the empty bottle and razor")

但是,在测试您的代码时,我意识到您放置逻辑的方式将无法正常工作。

使用 if all(x in inventory for x in ['comb','razor']) 将正确处理两个变量的存在,combrazorinventory 中,如果缺少其他值之一,则允许以正确的方式推出其他条件.

inventory = ['comb','razor']
#inventory = ['razor','']
#inventory = ['comb']

print("You are back in your cell. You saw your bed, broken sink, grotty toilet, cut up ju
mpsuit")
if all(x in inventory for x in ['comb','razor']):
    print ("and the table with the empty bottle.")
elif ('comb' not in inventory) and ('razor' in inventory):
    print("and the table with the empty bottle and comb.")
elif ('razor' not in inventory) and ('comb' in inventory):
    print("and the table with the empty bottle and razor")

【讨论】:

    【解决方案2】:
    print ("You are back in your cell. You saw your bed, broken sink, grotty toilet, cut up jumpsuit")
        if "comb" and "razor" in inventory:
            print ("and the table with the empty bottle.")
        elif "comb" not in inventory and "razor" in inventory:
            print ("and the table with the empty bottle and comb.")
        elif "razor" not in inventory and "comb" in inventory:
            print ("and the table with the empty bottle and razor")
    

    【讨论】:

      猜你喜欢
      • 2011-03-11
      • 1970-01-01
      • 2020-10-16
      • 1970-01-01
      • 1970-01-01
      • 2015-09-24
      • 1970-01-01
      • 2012-10-15
      • 2014-04-06
      相关资源
      最近更新 更多