Home > Mobile >  Piece of code on a python project won't run as intended
Piece of code on a python project won't run as intended

Time:11-30

There's a small section in my python school project that isn't running as i intend:

elif "eat berry" in command:
    if inventory == 'berry':
        typingPrint("You eat a berry")
        Penguin.health = Penguin.health   10
        print("Your health is now", Penguin.health)
        userInput()
    else:
        typingPrint("sorry you can't eat that right now.")
        userInput()

when i run this code even if i have a berry in the inventory (the inventory is created as a list), it still prints "Sorry you can't eat that right now." Any idea on how to fix this? Thank you in advance.

CodePudding user response:

You should use the in operator, since ["berry"] != "berry"

if "berry" in inventory:

CodePudding user response:

Since your inventory is a list, then you need to change this
if inventory == 'berry': into
if 'berry' in inventory :

CodePudding user response:

If inventory is of type list, then you should check that the item in question, "berry", is in the list inventory:

if "berry" in inventory:
    # do something
  • Related