Search code examples
pythoninventorytext-based

Inventory help Python


So I'm trying to arrange how the gameplay of my text base game will function if the player may/not have certain items in their inventory.

 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")

it is telling me that I have a syntax error in this line of code

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

I can't seem to see what mistake I have made, I am a beginner so perhaps there is an alternative way of executing my needs.


Solution

  • You are almost there

    The else works only that way

    else:
        do something
    

    So your code would be like that

     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")
    

    Or that

     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")
    

    However, when testing your code, I realized that the way you've put your logic will not work correctly.

    Using that if all(x in inventory for x in ['comb','razor'])will treat correctly the presence of the two variables, comb and razor in inventory and allow the other conditions to be rolled out in a correct manner if one of the other value was missing.

    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")