Reputation: 11
So i am creating a very basic text based game, however i want the answers the program gives to be relatively correct in response to different inputs. Instead of having to tell the program to write a specific thing for each different answer, i want to be able to import a dictionary of types into the script and have keywords for the different groups of words (like negative and positive connotations). Is this possible? Everything i have read hasn't really answered my question or done what i wanted...
my mock up scripts below:
Diction included:
a=raw_input("Are you well?")
if a=='yes':
print("Good to hear.")
elif a=='yep":
print("Good to hear!")
#I don't want to put it all in manually or use 'or'
elif a=='no':
print("That's not good!")
elif a=='nope':
print("That's not good!")
else:
print("Oh.")
with dictionary:
NegConns=('no','nope','never','No','Nope')
#'no' etc. is what the user inputs, NegConns is the keyword
PosConns=('yes','sure','okay','fine','good')
import mydiction
question=raw_input("How are you?")
if question== NegConns:
print("That's not good.")
elif question==PosConns:
print("Good to hear.")
else:
print("oh.")
So basically if the input is negative the program is sympathetic, if the input is positive the program congratulates. I'm not sure if this is possible for exactly what i'm wanting or if i'm going about this the wrong way, and i can't seem to find help so i'm putting it out there... Thanks in advance.
Upvotes: 1
Views: 93
Reputation: 1333
This is almost correct, except you want to tweak your import statement a bit:
from mydiction import NegConns, PosConns
and change your equality tests to array-membership tests, like so:
if question in NegConns:
or,
if question in PosConns:
Also, you should check out https://docs.python.org/3/tutorial/controlflow.html#default-argument-values. The example code snippet looks almost exactly like the problem you're trying to solve.
Additionally, consider using dictionaries or sets instead of lists/tuples. You should get the benefit of O(1) lookup while using the in
operator on dictionaries/sets.
Upvotes: 1
Reputation: 48077
Let's say your directory structure is as:
--\my_project # Your directory containing the files
-| mydiction.py
-| testmod.py
Add a __init__.py
in my_project directory to make it as python module. Now your project structure will be like:
--\my_project
-| __init__.py
-| mydiction.py
-| testmod.py
Now in order to import
from mydiction.py to testmod.py, you have to mention in your testmod.py as:
from mydiction import NegConns, PosConns
Upvotes: 0
Reputation: 119
I think that's OK, but you could create a list with the Negative and Positive answers and check it during the if statement. See how it works:
negativeAnswers = ['No','Nope','I don't think so']
positiveAnswers = ['Yeah', 'Yes', 'Of Course']
question1 = raw_input('How are you?')
if question.lowercase() in positiveAnswers;
print('Nice to hear that!')
elif question.lowercase() in negativeAnswers:
print('Oh')
else:
print('Sorry. I didn't understand you. :(')
I hope it helped you.
Upvotes: 0