使用输入时,只有 if 语句有效,但 elif/else 无效

When using input, only the if statement works but not elif/else

使用输入时,它似乎只适用于我的 If 命令,但不适用于我的 elif/else 语句。我不认为这是语法或间距问题。如果能帮我解决这个问题,我将不胜感激。 我基本上只是让这个脚本通过输入是或否来自动化流程图。如果用户输入 Yes 或 No,则会出现以下提示。

import os
import sys
import logging

print ('Hello! Please type the age of the baby in days')
age = int(input())
if age >= 8 and age <= 21:
    print('Obtain urinalysis, blood culture, and perform LP. Many obtain inflammatory markers (IMs)')
    print('Increased HSV risk? Type Yes or No')
    HSV = input()
    if HSV == 'Y' or 'yes' or 'Yes' or 'YES':
        print('Send HSV studies.')
        print('1. Initiate parenteral antimicrobial(s), including acyclovir. 2. Observe in hospital.')
    elif HSV == 'N' or 'no' or 'n' or 'No' or 'NO':
        print('1. Initiate parenteral antimicrobial(s). 2. Observe in hospital.')
    else:
        print('Invalid answer')
    print('Was the pathogen or source identified? Type Yes or No')
    indentify = input()
    if indentify == 'Y' or 'yes' or 'y' or 'Yes' or 'YES':
        print('Treat infection')
        input()
    elif indentify == 'N' or 'no' or 'n' or 'No' or 'NO':
        print('1. Discontinue antimicrobial(s) and may discharge hospitalized infant if all cultures are negative at 24 to 36 hours and HSV PCR is negative (if sent). 2. Follow for duration of illness.')
        input()
    else:
        print('Invalid answer')
        input()
elif age >= 22 and age <= 28:
    print('Obtain urinalysis, blood culture, and IMs.')
    print('Is the urinalysis positive? Type Yes or No.')
    urinalysis = input()
    if urinalysis == 'Y' or 'yes' or 'y' or 'Yes' or 'YES':
        print('Send bladder catheterization or SPA urine culture.')
    print('Are IMs abnormal? Type Yes or No')
    ab_ims = input()
    if ab_ims == 'Y' or 'yes' or 'y' or 'Yes' or 'YES':
        print('Perform LP.')
        print('Was CSF obtained? Type Yes or No')
        CSF = input()
        if CSF == 'Y' or 'yes' or 'y' or 'Yes' or 'YES':
            print('CSF pleocytosis or uninterpretable? Type Yes or No')
            CSF_pleo = input()
            if CSF_pleo == 'Y' or 'yes' or 'y' or 'Yes' or 'YES':
                print('1. Administer parenteral antimicrobial(s) and 2. Observe in hospital.')
                print('Was the pathogen or source identified? Type Yes or No')
                indentify = input()
                if indentify == 'Y' or 'yes' or 'y' or 'Yes' or 'YES':
                    print('Treat infection.')
                    input()
                elif indentify == 'N' or 'no' or 'n' or 'No' or 'NO':
                    print('1. Discontinue antimicrobial(s) and may discharge hospitalized infant if all cultures are negative at 24 to 36 hours and HSV PCR is negative (if sent). 2. Follow for duration of illness.')
                    input()
                else:
                    print('Invalid answer.')
            elif CSF_pleo == 'N' or 'no' or 'n' or 'No' or 'NO':
                print('Will observation be at home? Type Yes or No')
                home = input()
                if home == 'Y' or 'yes' or 'y' or 'Yes' or 'YES':
                    print('1. Administer parenteral antimicrobial(s). 2. Observe at home. 3. Reassess in 24 hours.')
                    if identify == 'Y' or 'yes' or 'y' or 'Yes' or 'YES':
                        print('Treat Infection')
                        input()
                    else:
                        pass
                elif identify == 'N' or 'no' or 'n' or 'No' or 'NO':
                    print('1. Discontinue antimicrobial(s) and may discharge hospitalized infant if all cultures are negative at 24 to 36 hours and HSV PCR is negative (if sent). 2. Follow for duration of illness.')
                    input()
                else:
                    print('Invalid answer.')
    elif CSF == 'N' or 'no' or 'n' or 'No' or 'NO':
        print('1. Administer parenteral antimicrobial(s) and 2. Observe in hospital.')
        print('Was the pathogen or source identified? Type Yes or No')
        indentify = input()
        if identify == 'Y' or 'yes' or 'y' or 'Yes' or 'YES':
            print('Treat infection.')
            input()
        elif identify == 'N' or 'no' or 'n' or 'No' or 'NO':
            print('1. Discontinue antimicrobial(s) and may discharge hospitalized infant if all cultures are negative at 24 to 36 hours and HSV PCR is negative (if sent). 2. Follow for duration of illness.')
        else:
            print('Invalid answer.')
            input()
    elif ab_ims == 'N' or 'no' or 'n' or 'No' or 'NO':
        print('May perform LP.')
        LP = input()
        print('Was the LP performed? Type Yes or No')
        if LP == 'Y' or 'yes' or 'y' or 'Yes' or 'YES':
            CSF = input()
        print('Was CSF obtained? Type Yes or No')
        if CSF == 'Y' or 'yes' or 'y' or 'Yes' or 'YES':
            print('CSF pleocytosis or uninterpretable? Type Yes or No')
            CSF_pleo = input()
            if CSF_pleo == 'Y' or 'yes' or 'y' or 'Yes' or 'YES':
                print('1. Administer parenteral antimicrobial(s) and 2. Observe in hospital.')
                print('Was the pathogen or source identified? Type Yes or No')
                indentify = input()
                if identify == 'Y' or 'yes' or 'y' or 'Yes' or 'YES':
                    print('Treat infection.')
                elif identify == 'N' or 'no' or 'n' or 'No' or 'NO':
                    print('1. Discontinue antimicrobial(s) and may discharge hospitalized infant if all cultures are negative at 24 to 36 hours and HSV PCR is negative (if sent). 2. Follow for duration of illness.')
                    input()
                else:
                    print('Invalid answer.')
                    input()
        elif CSF == 'N' or 'no' or 'n' or 'No' or 'NO':
            print('1. Administer parenteral antimicrobial(s) and 2. Observe in hospital.')
            print('Was the pathogen or source identified? Type Yes or No')
            indentify = input()
            if identify == 'Y' or 'yes' or 'y' or 'Yes' or 'YES':
                print('Treat infection.')
            elif identify == 'N' or 'no' or 'n' or 'No' or 'NO':
                print('1. Discontinue antimicrobial(s) and may discharge hospitalized infant if all cultures are negative at 24 to 36 hours and HSV PCR is negative (if sent). 2. Follow for duration of illness.')
            else:
                print('Invalid answer')
                input()
    else:
        pass
elif age >= 29 and age <= 60:
    print('Obtain urinalysis, blood culture, and IMs.')
    print('Were IMs elevated? Type Yes or No')
    ab_ims = input()
    if ab_ims == 'Y' or 'yes' or 'y' or 'Yes' or 'YES':
        print('1. Send bladdder catherization or SPA urine culture if positive urinalysis. 2. May perform LP. 3. If CSF result is positive: a. Addminister parenteral antimicrobial(s) and b. Observe closely in hospital.')
        print('4. If CSF result is negative and either urinalysis negative or positive: a. May administer parenteral or oral microbial(s) and b. May observe closely in hospital or at home.')
        print('5. If CSF is not available or uninterpretable: a. Administer parenteral antimicrobial(s) and b. May observe closely in hospital or at home.')
    elif ab_ims == 'N' or 'no' or 'n' or 'No' or 'NO':
        print('Is urinalysis positive?')
        urinalysis = input()
        if urinalysis == 'Y' or 'yes' or 'y' or 'Yes' or 'YES':
            print('1. Send bladder catherization or SPA urine culture. 2. Need not perform LP. 3. Administer oral antimicrobial(s). 4. May observe closely ata home. 5. Follow-up in 12 to 24 hours.')
        elif urinalysis == 'N' or 'no' or 'n' or 'No' or 'NO':
            print('1. Need not perform LP. 2. Need not administer antimicrobial(s). 3. Observe closely at home. 4. Follow-up within 24-36 hours.')
        else:
            print('Invalid answer.')
            input()
    else:
        pass
    print('Was the pathogen or source identified at 24 to 36 hours? Type Yes or No')
    identify = input()
    if identify == 'Y' or 'yes' or 'y' or 'Yes' or 'YES':
        print('Was the source limited to urine? Type Yes or No')
        source = input()
        if source == 'Y' or 'yes' or 'y' or 'Yes' or 'YES':
            print('1. Complete treatment with oral antimicrobials. 2. Ddischarge hospitalized infants. 3. Manage for duration of illness.')
            input()
        elif source == 'N' or 'no' or 'n' or 'No' or 'NO':
            print('Treat infection.')
            input()
        else:
            print('Invalid answer')
            input()
    elif identify == 'N' or 'no' or 'n' or 'No' or 'NO':
        print('1. Discontinue antimicrobials if administered. 2. Discharge hospitalized infants. 3. Manage for duration of illness.')
        input()
    else:
        print ('Invalid answer')
        input()
else:
    print ('Invalid answer. Baby must be 8-60 days old.')
    input()

问题出在你的条件上,它总是产生一个真值。

表达式 identify == 'Y' or 'yes' or 'y' or 'Yes' or 'YES' 将 return 为真(如果标识 == 'Y')或将 return 'yes'.

两者(True 和 'yes')都是真值。

您想将表达式 identify == 'Y' or 'yes' or 'y' or 'Yes' or 'YES' 替换为 identify in ['Y', 'yes', 'y', 'Yes', 'YES']