我有以下内容:
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.")
这对应于:
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')")
所以我有一个最小的数据库和一个问卷形式,我试图让问题与数据库中的答案有关,然后告诉我它是哪种疾病然后将其打印到文本文件中。然而,我是这一切的新手,我正在努力解决这一切,我老实说真的只是卡住了,不确定从哪里开始。我们将不胜感激。
编辑:我收到的错误消息是:object has no attribute 'get'
答案 0 :(得分:1)
如果您将行q = (question_data)
更改为q = question_data[0]
,它应该会让您走上正确的轨道。更有用的替代方案是将其置于for
循环中:
for q in question_data:
#process the question
这需要更改dict
,以便所有dict
中的密钥都相同。
更高级的选项是将问题信息存储在数据库中,或者存在于您拥有的同一个表中,或者存储在与现有表的外键关系连接的另一个表中。
答案 1 :(得分:1)
行\label{fig:abc}
相当于q = (question_data)
。
现在q = question_data
/ q
是一个列表,但是当你调用question_data
(发生错误消息的地方)时,你会认为它是一个字典。最简单的解决方案是将dataformat更改为字典,如下所示:
q.get()
然后你可以做类似的事情:
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"},
}
}