I'm creating a text-based game in python and have hit a road block. I have a function that checks if user input contains certain words, and if it does it returns the user-input, otherwise it will re-ask for the input. If you write something that doesn't contains one of the words, it will re-call the function.
def contains_words(prompt, words):
user_input = raw_input(prompt).strip().lower()
if user_input == "instructions":
print
instructions()
print
contains_words(prompt, words)
elif user_input == "i" or user_input == "inventory":
if len(inventory) == 0:
print
print "There is nothing in your inventory."
print
contains_words(prompt, words)
else:
print "Your inventory contains: " + inventory
contains_words(prompt, words)
else:
if user_input in words:
return user_input
else:
print
print "I did not understand your answer, consider rephrasing."
contains_words(prompt , words)
Here is me calling it:
pizza = contains_words("Do you like pizza?", ["yes", "no"])
Within this function you can bring up the instructions or your inventory and then it will recall the function. Everything works like normal if you put one of the words in your answer the first time you are asked. The problem happens when you enter something incorrect, bring up the inventory or bring up the instructions. It causes the function to return nothing, instead of user-input. Why is this happening? Is it because the function resets so the parameter equal none?