0

Just began coding a few weeks ago so forgive my ignorance.

I want to make it so that my IF statement will recognize a keyword from the user input rather then it having to be an exact match.

So instead of the user having to type exactly "Light" they could type something like "Go to the light" and the IF statement will still recognize "Light" as the keyword and proceed through that IF or ELIF statement.

def start_room():
    if not power:
        print("You are in pitch darkness, the only thing you can see is a faint green glow from beneath the silhouette of a door to your left.")
        print("What will you do?")
        choice = input("> ")

        if choice == "Light":
            print("yadayadayada")
atline
  • 16,106
  • 12
  • 44
  • 65

3 Answers3

1

The following will work

def start_room():
    if not power:
        print("You are in pitch darkness, the only thing you can see is a faint green glow from beneath the silhouette of a door to your left.")
        print("What will you do?")
        choice = input("> ")

    if any([True for i in ['light', 'left'] if i in choice.lower()]):
        print("yadayadayada")
Jeril
  • 5,663
  • 3
  • 39
  • 62
1

You can try doing like this:

if "Light" in choice:
    print("yadayadayada")
gg1
  • 70
  • 9
0

You want to use in keyword. Try if "light" in choice:

JJAACCEeEKK
  • 196
  • 1
  • 11