0

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'

2
  • 1
    What specifically are you having trouble with? opening and writing to a text file? does what you have so far run? give expected results? Commented Mar 14, 2018 at 22:56
  • Using the database inside the questionnaire then printing it out. So far it does not work with the problem as object has no attribute 'get'. Just in general not sure whats the next move. Commented Mar 14, 2018 at 23:00

2 Answers 2

1

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

5 Comments

Hey, sorry for the late reply but it's still not liking the whole get statement. Throwing up a whole: AttributeError: 'str' object has no attribute 'get'
On which line of code? I just fixed a small mistake in my answer but it doesn't explain the error you see
The line of : question1 = q.get('question1')
Then whatever is in q is wrong. Do you still have q=(question_data) and did you put question_data in the form I put in my question? differences there are the only explanation for why it works for me and not for you.
Yes i changed it. Then i added for q in question_data: to above the getting the dictonary, but if i dont add that i get the same error but worse.
1

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 dicts so that the keys are the same in all the dicts.

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.

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.