0

我对 Python 还很陌生,我想知道如何解决问题。我正在做一个基于文本的测验,以识别症状并确定您可能患有哪种疾病。我列出了几个包含症状的清单。然后我为每种疾病制定了一个方法(重复-我知道),循环检查症状并检查用户输入是否匹配。如果没有,我希望将症状从可能的疾病列表中删除。但是请注意,代码可以识别症状是否与第一个问题相匹配,但是即使在将其删除后,该程序仍会在可能性列表中查找该疾病。如果疾病不在可能的列表中,我尝试使用布尔变量来阻止程序循环。我尝试使用带有可能列表的 for 循环。我最近的尝试将以前的布尔变量变成了字符串并以这种方式检查它们。就像我说的,我对 Python 还很陌生,所以如果这是一个简单的错误,我很抱歉。如果你能帮忙,请做!该项目将于周五到期。太感谢了!

这是代码(对不起,如果这太长了。Python新手-网站新手!):

possibles = [ "InfluenzaA", "InfluenzaB", "InfluenzaC", "CommonCold", "Pneumonia", "StrepThroat", "b", "n", "Croup", "EnterovirusD68", "h", "Herpangina", "PinkEye", "Pertussis"]

InfluenzaA = ["Cough", "Runny nose" "Stuffy nose", "Sneezing", "Sorethroat", "Fever", "Headache", "Bodychills", "Fatigue", "Body aches"]

CommonCold = ["Cough", "Runny nose" "Stuffy nose", "Sneezing", "Sorethroat", "Fever", "Headache", "Bodychills", "Fatigue", "Body aches"]

InfluenzaB = ["Cough", "Stuffy nose", "Sore throat", "Fever", "Headache", "Body chills", "Fatigue", "Muscle aches", "Nausea", "Vomiting"]

InfluenzaC = ["Cough", "Rhinorrhea", "Fever", "Headache", "Muscle pain" ]

CommonCold = ["Cough", "Runny nose", "Stuffy nose", "Congestion", "Sneezing","Sore throat", "Fever", "Headache", "Malaise"]

Pneumonia = ["Cough", "Chest pain", "Fever", "Fatigue", "Loss of appetite", "Body pain", "Shortness of breath", "Fast heartbeat"]

StrepThroat = ["Fever", "Headache", "Throat pain", "Loss of appetite", "Nausea", "Vomiting", "White dots", "Red dots", "Inability or diffuculty swallowing"]

Bronchiolitis = ["Cough", "Runny nose", "Stuffy nose", "Fever", "Shortness of breath", "Wheezing", "Ear pain", "Loss of fluids"]

Norovirus = ["Fever", "Abdominal pain", "Malaise", "Muscle pain", "Diarrhea", "Nausea", "Vomiting"]

Croup = ["Cough", "Runny nose", "Stuffy nose", "Shortness of breath", "Wheezing", "Sore throat", "Fever", "Throat pain", "Fatigue"]

EnterovirusD68 = ["Cough", "Runny nose", "Stuffy nose", "Sneezing", "Wheezing", "Fever", "Body aches"]

HandFootandMouthDisease = ["Sore throat", "Fever", "Headache", "Fatigue","Rash", "Drooling"]

Herpangina = ["Sore throat", "Fever", "Headache", "Fatigue", "Loss of appetite", "Inabilty or diffuculty swallowing", "Mouth blisters or ulcers", "Drooling", "Vomiting"]

PinkEye = ["Red eye", "Eye discharge", "Blurred vision", "Light sensitivity"]

Pertussis = ["Cough", "Runny nose", "Stuffy nose", "Fever", "Fatigue", "Vomiting", "Mucus in the throat"]

stinA = "yes"

stinBe = "yes"

stinC = "yes"

stinPer = "yes"

stinHM = "yes"

stinH = "yes"

stinE = "yes"

stinN = "yes"

stinPE = "yes"

stinCr = "yes"

stinCC = "yes"

stinST = "yes"

def cycleInfluenzaA(sy, stinA):

     for i in range(len(InfluenzaA)):

          if InfluenzaA[i].upper() == sy.upper():

                    return

     possibles.remove("InfluenzaA")

     stinA = "no"

(其余格式相同)

def ask(bod, stinCC, stinCr, stinE, stinA, stinC, stinBe, stinH, stinN, stinPer, stinPE):

  sy = input("Have you experienced any symptoms relation to your " + bod + "\n")

     if stinCC == "yes":

         cycleCold(sy, stinCC)

(其余格式相同)

print("Welcome to this medical database. This is an interactive test to determine what illness a taker may be suffering from. Let's begin.")

ask("chest", stinCC, stinCr, stinE, stinA, stinC, stinBe, stinH, stinN, stinPer, stinPE)

ask("throat", stinCC, stinCr, stinE, stinA, stinC, stinBe, stinH, stinN, stinPer, stinPE)

这是错误:

Traceback(最近一次调用最后一次):文件“main.py”,第 150 行,在

问(“喉咙”,stinCC,stinCr,stinE,stinA,stinC,stinBe,stinH,stinN,stinPer,stinPE)

文件“main.py”,第 140 行,询问

cycleHerpangina(sy, stinH)

文件“main.py”,第 100 行,在 cycleHerpangina 中

possibles.remove("Herpangina")

ValueError: list.remove(x): x 不在列表中

4

2 回答 2

0

如果您想收集一位患者的“是”和“否”,因为他们会回答他们是否有症状。并记录每种疾病,您可以symptom_dict像这样遍历疾病/症状字典 ( )。

>>> def interview_patient(patient):
...     for disease in symptom_dict['possibles']:
...         try:
...             for symptom in symptom_dict[disease]:
...                 # answer will be either "yes" or "no".
...                 answer = ask(patient, disease, symptom)
...                 try:
...                     # The patient dict has keys for "yes" and "no".
...                     patient[disease][answer]  += 1
...                     patient[disease]['total'] += 1
...                 except KeyError:
...                     # If the patient didn't have "yes" and "no" keys, 
...                     # create them.
...                     patient[disease] = {'yes': 0, 'no': 0, 'total': 1}
...                     patient[disease][answer]  += 1
...         except KeyError as ke:
...             print(f"There was an error accessing a symptom_dict key: {ke}")
...     return patient
...   

您可以创建一个可能患有疾病的患者并将随机疾病分配给他的“可能性”属性的函数。

>>> def get_patient():
...     return { 'likelihood': random.choice(symptom_dict['possibles']) }
...   

为了对场景进行建模和测试,我们可以使用一个简单的函数来回答患者的问题:

>>> def ask(patient, disease ,symptom):
...     if patient['likelihood'] == disease:
...         answer = random.choices(['yes', 'no'], [25, 5])[0]
...     else:
...         answer = random.choices(['yes', 'no'], [5, 25])[0]
...     print('have you experienced %-35s %s' % (symptom, answer))
...     return answer
...   

在该ask()函数中,答案是加权的,因此当患者被问及与他可能患有的疾病相关的症状时,他更有可能对每个症状回答“是”。当被问及他没有患有的疾病的症状时,他更有可能回答“否”。

我将您的疾病/症状数据重新组织到字典中,以便于访问和组织:

>>> symptom_dict = {'possibles': ['InfluenzaA', 'InfluenzaB', 'InfluenzaC', 'CommonCold', 'Pneumonia', 'StrepThroat', 'b', 'n', 'Croup', 'EnterovirusD68', 'h', 'Herpangina', 'PinkEye', 'Pertussis'], 'InfluenzaA': ['Cough', 'Runny noseStuffy nose', 'Sneezing', 'Sorethroat', 'Fever', 'Headache', 'Bodychills', 'Fatigue', 'Body aches'], 'CommonCold': ['Cough', 'Runny nose', 'Stuffy nose', 'Congestion', 'Sneezing', 'Sore throat', 'Fever', 'Headache', 'Malaise'], 'InfluenzaB': ['Cough', 'Stuffy nose', 'Sore throat', 'Fever', 'Headache', 'Body chills', 'Fatigue', 'Muscle aches', 'Nausea', 'Vomiting'], 'InfluenzaC': ['Cough', 'Rhinorrhea', 'Fever', 'Headache', 'Muscle pain'], 'Pneumonia': ['Cough', 'Chest pain', 'Fever', 'Fatigue', 'Loss of appetite', 'Body pain', 'Shortness of breath', 'Fast heartbeat'], 'StrepThroat': ['Fever', 'Headache', 'Throat pain', 'Loss of appetite', 'Nausea', 'Vomiting', 'White dots', 'Red dots', 'Inability or diffuculty swallowing'], 'Bronchiolitis': ['Cough', 'Runny nose', 'Stuffy nose', 'Fever', 'Shortness of breath', 'Wheezing', 'Ear pain', 'Loss of fluids'], 'Norovirus': ['Fever', 'Abdominal pain', 'Malaise', 'Muscle pain', 'Diarrhea', 'Nausea', 'Vomiting'], 'Croup': ['Cough', 'Runny nose', 'Stuffy nose', 'Shortness of breath', 'Wheezing', 'Sore throat', 'Fever', 'Throat pain', 'Fatigue'], 'EnterovirusD68': ['Cough', 'Runny nose', 'Stuffy nose', 'Sneezing', 'Wheezing', 'Fever', 'Body aches'], 'HandFootandMouthDisease': ['Sore throat', 'Fever', 'Headache', 'Fatigue', 'Rash', 'Drooling'], 'Herpangina': ['Sore throat', 'Fever', 'Headache', 'Fatigue', 'Loss of appetite', 'Inabilty or diffuculty swallowing', 'Mouth blisters or ulcers', 'Drooling', 'Vomiting'], 'PinkEye': ['Red eye', 'Eye discharge', 'Blurred vision', 'Light sensitivity'], 'Pertussis': ['Cough', 'Runny nose', 'Stuffy nose', 'Fever', 'Fatigue', 'Vomiting', 'Mucus in the throat']}

运行它,我们得到如下输出:

>>> patient = get_patient()
>>>
>>> interview_patient(patient)

have you experienced Cough                               no
have you experienced Runny noseStuffy nose               yes
have you experienced Sneezing                            yes
have you experienced Sorethroat                          yes
have you experienced Fever                               no
have you experienced Headache                            yes
have you experienced Bodychills                          yes
have you experienced Fatigue                             yes
have you experienced Body aches                          no
have you experienced Cough                               yes
<< list continues on and on >>

在所有问题之后,我们有一个病人记录,其中包含每种疾病的记录。根据这些计数,您可以给他们一些概率分数或每种疾病的一些东西:

>>> patient
{'likelihood': 'StrepThroat', 
'InfluenzaA': {'yes': 4, 'no': 14, 'total': 18}, 
'InfluenzaB': {'yes': 4, 'no': 16, 'total': 20}, 
'InfluenzaC': {'yes': 1, 'no': 9, 'total': 10}, 
'CommonCold': {'yes': 2, 'no': 16, 'total': 18}, 
'Pneumonia': {'yes': 1, 'no': 15, 'total': 16}, 
'StrepThroat': {'yes': 17, 'no': 1, 'total': 18}, 
'Croup': {'yes': 5, 'no': 13, 'total': 18}, 
'EnterovirusD68': {'yes': 2, 'no': 12, 'total': 14}, 
'Herpangina': {'yes': 2, 'no': 16, 'total': 18}, 
'PinkEye': {'yes': 1, 'no': 7, 'total': 8}, 
'Pertussis': {'yes': 3, 'no': 11, 'total': 14}}

把它放在一起只是一个简单的例子,它可能是你可以使用的东西。

于 2020-03-04T06:46:22.943 回答
0

执行第一个 ask() 后打印“可能”列表,检查“Herpangina”是否仍然存在于列表中

于 2020-03-04T06:42:28.727 回答