Stagger
Stagger

Reputation: 115

Check if a string contains another string in python

So I am busy making a interactive text based game where the user can input answers in his/her own words.

I wrote a piece of code to print certain strings when certain words are entered by the user but i can't get it to work.

No matter what I input, it always prints "NOVA ENTERS THE SPACECRAFT" What am I doing wrong?

Below is my code snippet:

craft_cave = input(char_name + ": I should probably search the ship for supplies and explore this cave, which should I do first? ")

print("")

if "craft" or "Craft" or "ship" or "Ship" in craft_cave:
  print("NOVA ENTERS THE SPACECRAFT")
elif "cave" or "Cave" or "mountain" or "Mountain" in craft_cave:
  print("NOVA LOOKS AROUND THE CAVE")
else:
  print("Invalid answer")

Upvotes: 2

Views: 230

Answers (4)

codelessbugging
codelessbugging

Reputation: 2909

if "craft" or "Craft" or "ship" or "Ship" in craft_cave:

is evaluated as

if ("craft" or ("Craft" or ("ship" or ("Ship" in craft_cave) ) ) ):

"craft" is evaluated as a boolean value first, which gives True, so we have

if (True or ("Craft" or ("ship" or ("Ship" in craft_cave) ) ) ):

and since True or x is always True, the statement evaluates to

if True:

so the first if condition always evaluates to True.

You need to directly check for each word in each of the if conditions

spacecraft_keywords = ["craft", "Craft", "ship", "Ship"]
cave_keywords = ["cave", "Cave", "mountain", "Mountain"]

def contains_any(keywords, s):
    return any(keyword in s for keyword in keywords)

if contains_any(spacecraft_keywords, craft_cave):
    pass
elif contains_any(cave_keywords, craft_cave):
...

Upvotes: 0

Subial Ijaz
Subial Ijaz

Reputation: 93

Simpler way, put all words in list

craft_cave = input( ": I should probably search the ship for supplies and explore 
this cave, which should I do first? ")

print("")
list1 = ('craft', 'Craft', 'ship', 'Ship')
list2 = ('cave', 'Cave', 'mountain', 'Mountain')  

if any(word in craft_cave for word in list1):
   print("NOVA ENTERS THE SPACECRAFT")

elif any(word in craft_cave for word in list2):
   print("NOVA LOOKS AROUND THE CAVE")

else:
   print("invalid")

Upvotes: 1

Ben
Ben

Reputation: 6348

You need to check the words invidually. Try something like:

vehicle_words = ["craft", "Craft", "ship", "Ship"]
cave_words = ["cave", "Cave", "mountain", "Mountain"]
if any(w in craft_cave for w in vehicle_words):
    print("NOVA ENTERS THE SPACESHIP")
elif any(w in craft_cave for w in cave_words)
    print("NOVA LOOKS AROUND THE CAVE")
else:
    print("Invalid Answer")

Upvotes: 2

Yuval Pruss
Yuval Pruss

Reputation: 9826

Wrong condition, it check if the value of "craft" is true and it is. you should do something like this:

if "craft" in craft_cave or "Craft" in craft_cave or "ship" in craft_cave or "Ship" in craft_cave:
    # continue

Even better:

if any(map(lambda word: word in craft_cave, ("craft", "Craft", "ship", "Ship"))):
    # continue

Upvotes: 1

Related Questions