2

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")
1
  • As a separate tip, you might consider converting words to lowercase before your run the checks whenever that makes no difference; e.g. ("Ship".lower() in "ship") == True". Commented Dec 9, 2018 at 8:21

4 Answers 4

2

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")
Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

Thanks a lot! you cleared it up for me, makes complete sense. It works.
How would i do this in reverse?(Check is the user's input does not contain those words?
1

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

2 Comments

I tried this as well and is does not seem to work. still print NOVA ENTERS THE SPACECRAFT no matter what I type
You sure? coz i checked again above code prints "NOVA LOOKS AROUND THE CAVE" when enter cave... and invalid if none strings match
0
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):
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.