0

我有一些问题。在我的代码中,我要编写一个“图灵测试”来诊断患者。1.我真的觉得这比我写的要简单。2. 我什至没有做对。当我运行程序时,无论我选择什么,我都会不断获得第一个选项。3. 我似乎无法让我的程序停止运行。我尝试过退出、中断、退出、sys.exit、sys.exit() 和 sys.exit(0)。所以我的问题是:“有没有更简单的方法来做到这一点?”,“我的 if 和 elif 语句做错了什么?”,以及“当用户不想继续时,如何让程序结束? " 谢谢!

(我的程序还没有完成,但你明白了)

import sys

def doctor():

    print ("Welcome to medical diagnostics")
    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
    if ("Head"):
        raw_input ("Where does your head hurt? Scalp, Eyes, Nose, Cheeks, Ears, Mouth, Jaw, or none: ")
        if ("Scalp"):
            raw_input ("Are you experiencing Agitation, Bleeding, Fracturing, Bruising, Drainage, Fainting, Fever, Headaches, Migraines, Lumps, Lightheadedness, Dizziness, Swelling, or Seizures?: ")
            if ("Agitation"):
                raw_input ("It is possible that this is due to withdrawals, side effects of intoxication, side effects of methamphetamine use, dementia in head injury, or epilepsy. Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
            elif ("Bleeding"):
                raw_input ("You may have had some sort of trauma or injury. Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
            elif ("Fracturing"):
                raw_input ("It is possible that you are dealing with physical abuse, trauma or injury, or osteogenesis imperfecta. Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
            elif ("Bruising"):
                raw_input ("It is possible that this is due to a contusion, a hematoma, trauma or injury, botox injections, or thrombocytopenia. Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
            elif ("Drainage"):
                raw_input ("It is possible that this is due to an allergic reaction or one or several abscesses. Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
            elif ("Fainting"):
                raw_input ("It is possible that this is due to a heart rhythm disorder, atrial or aortal complications, heat exhaustion, low blood pressure, anemia, ADHD, dehydration, drug overdose, hyperventilation, low blood sugar, lyme disease, pulmonary embolism or hypertension, or a brain aneurysm. Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
            elif ("Fever"):
                raw_input ("It is possible that this is due to viral pharyngitis, acute sinusitis, influenza, bacterial pneumonia, gastroenteritis, middle ear infection, sunburn, urinary tract infection, meningitis, coxsackie virus infection, one or several abscesses, aspirin poisoning, borchial adenoma, Crohn's disease, cryptococcosis, drug overdose, drug withdrawals, epiglottitis, hepatitis A or B, inflammatory bowel disease, kidney infection, lupus, lyme disease, mononucleosis, mumps, osteomyelitis, pericarditis, phlebitis, rheumatic fever, rheumatoid arthritis, sarcoidosis, scarlet fever, syphilis, thalassemia, thyroid storm, tuberculosis, valley fever, viral pneumonia, histoplasmosis, sickle cell disease, tick bite, brain infection, Chagas disease, dengue fever, legionella, malaria, toxic shock syndrome, typhoid fever, whooping cough, pilonidal cysts, measles, mesenteric lymphadenitis, plague, shingles, west nile virus, anthrax, endocarditis, or cat-scratch disease. Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
            elif ("Headaches"):
                raw_input ("It is possible that this is due to tension, acute sinusitis, acute stress reaction, caffeince withdrawals, type 1 or 2 diabetes, excessive caffeine use, malocclusion, nearsighedness, postconcussive syndrome, sleep apnea, sunburn, chronic sinusitis, meningitis, allergic reactions, anemia, astigmatism, botox injections, carbon monoxide poisoning, cervical spondylosis, chronic kidney disease, coxsackie virus infection, cryptococcosis, dementia in head injury, head injury, heat exhaustion, high blood pressure, hypocalcemia, insulin reaction, intracranial hematoma, lupus, lyme disease, mononucleosis, multiple sclerosis, mumps, nasal polyps, nonallergic rhinitis, noroviruses, ocular migraine, sporotrichosis, thalassemia, constipation, chemical burns, tick bite, anemia, brain aneurysm, brain infection, cyanide poisoning, endocarditis, lead poisoning, plague, pseudohypoparathyroidism, radiation sickness, typhoid fever, osteomyelitis, bird flu, brain tumor, shingles, or west nile virus. Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
            elif ("Migraines"):
                raw_input ("It is possible that this is due to a brain aneurysm or meningitis. Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
            elif ("Lumps"):
                raw_input ("It is possible that this is due to trauma or injury, abscesses, epidermal cysts, or benign lipoma. Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
            elif ("Lightheadedness"):
                raw_input ("It is possible that this is due to generalized anxiety disorder, heart rhythm disorder, hyperventilation, supraventricular tachycardia, acute kidney failure, anemia, heart complications, high or low blood pressure, labyrinthitis, thalassemia, or toxic shock syndrome. Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
            elif ("Dizziness"):
                raw_input ("It is possible that this is due to middle ear infection, labyrinthitis, benign paroxysmal positional vertigo, diabetes, fainting, generalized anxiety disorder, heart rhythm disorder, panic attack, anemia, heart complications, aspirin poisoning, benzodiazepine abuse, carbon monoxide poisoning, cryptococcosis, dehydration, heat exhaustion, high or low blood pressure, hyperventilation, insulin reaction, Meniere's disease, pulmonary hypertension, supraventricular tachycardia, chemical burns, anemia, cyanide poisoning, or small intestine cancer. Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
            elif ("Swelling"):
                raw_input ("It is possible that this is due to bee or wasp stings, insect bites or stings, poison ivy, oak, or sumac, burns, chemical burns, or osteomyelitis. Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
            elif ("Seizures"):
                raw_input ("It is possible that this is due to alcohol withdrawal, cocaine abuse, insulin reaction, barbiturate abuse, brain aneurysm, brain infection, brain tumor, Chigas disease, tetanus, or west nile virus. Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
            elif ("none"):
                raw_input ("Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
        if ("Eyes"):
            raw_input ("Are you experiencing Bleeding, Blurred Vision, Fracturing, Bruising, Double Vision, Drainage, Redness, Floating Spots, Scratchy Eyes, Irritation, Lumps, Bruising, Numbness, Red Eyelid, Swelling, or none?: ")
            if ("Bleeding"):
                raw_input ("You may have had some sort of trauma or injury. Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
            elif ("blurred vision"):
                raw_input ("It is possible that this is due to Diabetes, Eye injury, Nearsightedness, Pink eye, Acute angle-closure glaucoma, Astigmatism, Benzodiazepine abuse, Cryptococcosis, Diabetic ketoacidosis, Epilepsy, Foreign object in the eye, Glaucoma, Low blood pressure, Ocular migraine, Retinal detachment, Diabetic eye disease, or Pseudohypoparathyroidism. Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
            elif ("fracturing"):
                raw_input ("It is possible that this is due to a broken eye socket. Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
            elif ("bruising"):
                raw_input ("It is possible that this is due to a hematoma, black eye, botox injection, trauma or injury, or thrombocytopenia. Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
            elif ("double vision"):
                raw_input ("It is possible that this is due to diabetes, alcohol intoxication, aspirin poisoning, cataracts, botulinum poisoning, or Cavernous sinus thrombosis. Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
            elif ("drainage"):
                raw_input ("It is possible that this is due to pink eye or one or several abscesses. Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
            elif ("floating spots"):
                raw_input ("It is possible that this is due to benign eye floaters, diabetic disease, or retinal detachment. Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
            elif ("scratchy eyes"):
                raw_input ("It is possible that this is due to Dust exposure, Hay fever, Pink eye, Wind exposure, Dry eyes, Foreign object in the eye, Sarcoidosis, Sjogren's syndrome, or Bird flu. Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
            elif ("irritation"):
                raw_input ("It is possible that this is due to Allergic reaction, Indoor allergens, Hay fever, Rosacea, Eye allergies, or Chagas disease. Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
            elif ("lumps"):
                raw_input ("It is possible that this is due to one or several abscesses or chalzion. Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
            elif ("numbness"):
                raw_input ("It is possible that this is due to burns or chemical burns. Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
            elif ("swelling"):
                raw_input ("It is possible that this is due to Acute sinusitis, Allergic reaction, Hay fever, Pink eye, Bee or wasp stings, Eye allergies, Foreign object in the eye, Insect bites, Insect sting, Poison ivy, oak, or sumac, Burn, or Chemical burns. Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
            elif ("none"):
                raw_input ("Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
        if ("Nose"):
            raw_input ("Are you experiencing bruising, difficulty breathing, drainage, congestion, nosebleed, numbness, pain, swelling, tenderness, or none?: ")
            if ("bruising"):
                raw_input ("It is possible that this is due to . Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
            elif ("difficulty breathing"):
                raw_input ("It is possible that this is due to . Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
            elif ("drainage"):
                raw_input ("It is possible that this is due to . Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
            elif ("congestion"):
                raw_input ("It is possible that this is due to . Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
            elif ("nosebleed"):
                raw_input ("It is possible that this is due to . Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
            elif ("numbness"):
                raw_input ("It is possible that this is due to . Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
            elif ("pain"):
                raw_input ("It is possible that this is due to . Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
            elif ("swelling"):
                raw_input ("It is possible that this is due to . Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
            elif ("tenderness"):
                raw_input ("It is possible that this is due to . Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
            elif ("none"):
                raw_input ("Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
        if ("Cheeks"):
            raw_input ("")
            elif (""):
                raw_input ("It is possible that this is due to . Would you like to start over? Yay or Nay: ")
                if ("Yay"):
                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
                elif ("Nay"):
                    sys.exit(0)
        if ("Ears"):
            raw_input ("")
        if ("Mouth"):
            raw_input ("")
        if ("Jaw"):
            raw_input ("")
        if ("None"):
            raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
    if ("Neck"):
        raw_input ("")
    if ("Chest"):
        raw_input ("Where does your chest hurt? Sternum, Chest, or Lateral: ")
        if ("Sternum"):
            raw_input ("")
        if ("Chest"):
            raw_input ("")
        if ("Lateral"):
            raw_input ("")
    if ("Abdomen"):
        raw_input ("")
    if ("Back"):
        raw_input ("")
    if ("Arms"):
        raw_input ("")
    if ("Legs"):
        raw_input ("")
    if ("Buttocks"):
        raw_input ("")
    if ("Pelvis"):
        raw_input ("")
    if ("None"):
        raw_input ("Do you have a fever? Y or N: ")
        if ("Y"):
            raw_input ("Are you feeling nauseous? Yes or No: ")
            if ("Yes"):
                raw_input ("")
            if ("No"):
                raw_input ("")
        if ("N"):
            raw_input ("")


doctor()
#       elif (""):
#                raw_input ("It is possible that this is due to . Would you like to start over? Yay or Nay: ")
#                if ("Yay"):
#                    raw_input ("Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: ")
#                elif ("Nay"):
#                    sys.exit(0)
4

2 回答 2

5

您拥有的所有条件句都将评估为,True因为您正在评估字符串 "Head"的“真实性” ,"Scalp"依此类推。您想要的是将这些字符串与输入请求的结果进行比较。

area = raw_input ("Which area hurts? Head, Neck,...") 
if area == "Head":
    head_area = raw_input("Where does your head hurt?...")
    # etc
else:
    # etc

至于“我这样做对吗”,您的逻辑乍一看似乎是合理的。但是,当然有更简单的方法来做你正在做的事情。我最简单的想法是使用字典来保存所有可能的诊断路径:把它想象成一棵“树”(事实上,如果你还没有这样做,我建议你画一个决策树)。然后,您可以将“树”嵌入到 Python 字典中,并简单地使用字典键来填写模板字符串 ("Which area hurts? ...""It is possible that...")。

我的意思的一个非常简短的例子:

# First, design your diagnosis tree. This will hold all the real logic.
diagnosis_tree = {
    'head' : { # first "level"
        'scalp' : { # second "level"
            'bleeding' : 'trauma or injury', # condition and diagnosis
            'agitation' : 'withdrawals, intoxication, dementia, or epilepsy'
        }
    },
    'chest' : {
        'sternum' : {
            'bleeding' : 'shot in the chest'
        }
    }
}

# Now, just traverse the tree according to the user input.

# Get first "level"
area = raw_input('Which area hurts?\n' + 
                 '\n'.join(k for k in diagnosis_tree))
area_tree = diagnosis_tree.get(area) # look up first level in the tree.

# Get second level of detail
sub_area = raw_input('Where does your {0} hurt?\n'.format(area) + 
                     '\n'.join(k for k in area_tree))
sub_area_tree = area_tree.get(sub_area) # look up second level in the tree.

# Get condition
condition = raw_input('What are you experiencing?\n' + 
                      '\n'.join(k for k in sub_area_tree))
diagnosis = sub_area_tree.get(condition) # look up condition in the tree.

# Print the final diagnosis
print('This could be due to {0}'.format(diagnosis))
于 2013-11-04T21:39:39.570 回答
1

您要求输入,raw_input()但您没有存储用户提要!您应该存储该值,然后对其执行if... else

像下面这样的东西

area = raw_input("Where does it hurt?")
if area == "chest":
    # do/ask something related to chest
elif area == "neck":
    # do/ask something related to neck
...
else:
    print ("Please specify one of these - chest/neck/...")

此外,您可以在开始时将问题存储到变量中,然后通过变量使用它们,这样您的代码看起来更漂亮。

ques_area = "Which area hurts? Head, Neck, Chest, Abdomen, Back, Arms, Legs, Buttocks, Pelvis, or none: "
ans_area = raw_input(ques_area)
于 2013-11-04T21:39:20.557 回答