2013-11-04 121 views
0

我遇到一些问题。在我的代码中,我要写一个诊断病人的“图灵测试”。我真的认为这比写作它更简单。我甚至没有这样做。当我运行这个程序时,无论我选择什么,我都会得到第一个选项。我似乎无法让我的程序停止运行。我试过quit,break,exit,sys.exit,sys.exit()和sys.exit(0)。所以我的问题是:“有没有更简单的方法来做到这一点?”,“我的if和elif语句有什么问题?”,以及“当用户不想继续时,如何让程序结束? “谢谢!“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) 
+3

使用原始输入,你需要存储在一个变量的反应,然后比较。例如'answer = raw_input('什么伤害?')'。然后,您可以将其与其他字符串进行比较,例如'if answer =='head'):'。现在,你正在评估字符串的真值,它总是真实的,除非是空的。 – hunse

+3

可能我建议添加一个变量来保存文本“哪个区域疼?头部,颈部,胸部,腹部,背部,手臂,腿部,臀部,骨盆或无:”所以你不要在代码中重复100次? –

+0

你可能想看看决策树... –

回答

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) 
5

你所有的条件句,因为有他们,会因为你评估的"Head""Scalp"等的“感实性”评估为True。你想要的是将这些字符串与输入请求的结果进行比较。

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

至于“我在做这个权利”,你的逻辑似乎是声音一目了然。但是,肯定有更简单的方法来做你正在做的事情。我最简单的想法是使用字典来保存所有可能的诊断路径:将其看作“树”(事实上,如果您尚未这样做,我建议您画一个decision tree)。然后,您可以将“树”嵌入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)) 
+0

哇!棒极了!感谢您向我展示一些更有效的方法!另外,一旦我纠正自己并保存值,我是否使用exit或sys.exit? –

+0

@ TravisJ.Land为了您的目的(和大多数目的,诚实),他们是完全一样的。 (请参阅文档](http://docs.python.org/2/library/sys.html#sys.exit)了解更多详情。但是,如果要在用户想要退出之前保持循环,那么只需将整个用户交互流程置于“while True:”中,然后在用户想要退出时将其“断开”即可。 –