Reputation: 25
I have the following:
import sqlite3
# connecting to the database
conn = sqlite3.connect("illness.db")
question_data = [
{
"question1": "Have you consumed Alcoholic drinks in the last 24 hours?t",
"choices": {"a": "Yes", "b": "No"},
"answer": "a"
},
{
"question2": "Another Question",
"choices": {"a": "choice 1", "b": "choice 2", "c": "choice 3"},
}
]
q = (question_data)
print(q.get('question1'))
answer = input(q.get('choices')).lower()
if answer == q.get('answer'):
c = conn.execute("SELECT illnessID, illness, illnessinfo from illnesses WHERE illness = 'Alcohol Misuse'")
else:
print("Okay Next question.")
This corresponds to:
def create_table():
c.execute("CREATE TABLE IF NOT EXISTS illnesses(illnessID PRIMARY KEY, illness VARCHAR(30), illnessinfo VARCHAR(50))")
c.execute("CREATE TABLE IF NOT EXISTS symptoms(symptomID PRIMARY KEY, symptom VARCHAR(50))")
def data_entry():
c.execute("INSERT INTO illnesses(illnessID, illness , illnessinfo) VALUES(1,'Flu','Influenza - Common Cold.')")
c.execute("INSERT INTO illnesses(illnessID, illness , illnessinfo) VALUES(2,'Acne','Skin Condition')")
c.execute("INSERT INTO illnesses(illnessID, illness , illnessinfo) VALUES(3,'Alcohol Misuse','Hangover')")
c.execute("INSERT INTO symptoms (symptomID,symptom) VALUES(1,'Headache')")
c.execute("INSERT INTO symptoms (symptomID,symptom) VALUES(2,'Spots')")
c.execute("INSERT INTO symptoms (symptomID,symptom) VALUES(3,'Breathing problems')")
So there I have a minimal DB and a form of a questionnaire which I'm trying to have questions relate to answers inside the DB which then tell me which illness it is then print that out into a text file. However I am new to all this and i'm trying to figure it all out and I'm honestly just really stuck and unsure where to go from here. Any help at all from this would be appreciated.
EDIT: The error message I get is: object has no attribute 'get'
Upvotes: 0
Views: 79
Reputation: 11869
The line q = (question_data)
is equivalent to q = question_data
.
Now q
/question_data
is a list, but you are treating it is a dictionary when you call q.get()
(which is where the error message occurs). The simplest solution is to change you dataformat to a dictionary, like this:
question_data = {
"question1": {
"question": "Have you consumed Alcoholic drinks in the last 24 hours?t",
"choices": {"a": "Yes", "b": "No"},
"answer": "a"
},
"question2":{
"question": "Another Question",
"choices": {"a": "choice 1", "b": "choice 2", "c": "choice 3"},
}
}
Then you can do something like:
# get the dictionary for question 1 from the dictionary of questions
question1 = q.get('question1')
# print the question
print(question1.get('question'))
# use the question 1 choices to get an answer
answer = input(question1.get('choices')).lower()
# check if answer is in the list of options (uses dictionary keys a/b/etc)
# swap to .values() if you want to compare to list of values ("like yes/no")
if answer == question1.get('answer'):
...
Upvotes: 1
Reputation: 1450
if you change your line q = (question_data)
to q = question_data[0]
it should get you on the right track. A more useful alternative would be to put that in a for
loop:
for q in question_data:
#process the question
that will require changing your dict
s so that the keys are the same in all the dict
s.
A still more advanced option is to store the question information in the database, either in the same table that you have, or in another table connected with a foreign key relationship to your existing table.
Upvotes: 1