“Elif”语句不能正常工作

2024-05-29 10:07:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我有些问题。在我的代码中,我将编写一个用于诊断患者的“图灵测试”。1我真的觉得这比我写的要简单。2我做得不对。当我运行程序时,我总是得到第一个选项,不管我选择什么。三。我似乎不能让我的程序停止运行。我试过退出,休息,退出,系统出口, 系统出口(),和系统出口(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)

Tags: ortononeyouinputrawifis
2条回答

所有的条件语句,如您所拥有的,将计算为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

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

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

^{pr2}$

您使用raw_input()请求输入,但您没有存储用户提要! 您应该存储该值,然后对其执行ifelse。在

像下面这样的东西

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/...")

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

^{pr2}$

相关问题 更多 >

    热门问题