2013-11-04 129 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:」中,然後在用戶想要退出時將其「斷開」即可。 –