“Elif”声明无法正常工作

时间:2013-11-04 21:36:38

标签: python

我遇到了一些问题。在我的代码中,我将编写一个用于诊断患者的“图灵测试”。我真的认为这比我写作更简单。我甚至没有这样做。当我运行程序时,无论我选择什么,我都会继续获得第一个选项。我似乎无法让我的程序停止运行。我试过quit,break,exit,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)

2 个答案:

答案 0 :(得分:5)

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

"Scalp"

至于“我做得对吗”,你的逻辑似乎一目了然。但是,肯定有更容易的方式来做你正在做的事情。我最简单的想法是使用字典来保存所有可能的诊断路径:将其视为“树”(事实上,如果您还没有这样做,我建议绘制decision tree)。然后,您可以将“树”嵌入Python字典中,只需使用字典键填写模板字符串(area = raw_input ("Which area hurts? Head, Neck,...") if area == "Head": head_area = raw_input("Where does your head hurt?...") # etc else: # etc "Which area hurts? ...")。

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

"It is possible that..."

答案 1 :(得分:1)

您要求raw_input()输入,但您没有存储用户Feed! 你应该存储这个价值&然后对其进行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)