有没有办法让 if 语句更短?

时间:2021-04-17 03:20:19

标签: python function loops if-statement

我让用户输入过敏症状并告诉用户该症状是过敏症状,如果症状在元组中,如果不在元组中,则它会告诉他们“不是过敏症状。我试过了使用元组并写入“if user_input ==“tuple_name”:”,但它不起作用。有没有办法做到这一点?如果元组不起作用,是否有办法使 if else 语句更短?

这是目前为止的小程序:

# most common questions asked by patients at pharmacy
# allergy treatment
def symptoms(allergies):
while True:
    user_input = input("enter symptom: ")
    if user_input == "runny nose":
        print("allergy symptom")
    elif user_input == "itchy throat":
        print("allergy symptom")
    elif user_input == "watery eyes":
        print("allergy symptom")
    elif user_input == "itchy nose":
        print("allergy symptom")
    elif user_input == "trouble breathing":
        print("this is a severe allergic reaction. Call 911!")
        break
    elif user_input == "hives":
        print("this is an allergic reaction")
    elif user_input == "rash":
        print("this is an allergic reaction")
    elif user_input == "throat closing":
        print("this is a severe allergic reaction. Call 911!")
        break
    elif user_input == "swelling":
        print("this is an allergic reaction")
    else:
        print("not an allergy symptom.")


symptoms('allergies')


# Rph recommended otc products for mild allergic reactions and for allergies
def allergy_otc():
while True:
    pt_otc_age = input("Is the patient younger than 12 years old? ")
    if pt_otc_age == "yes":
        print("Recommended: Children's Zyrtec, Claritin, Allegra, & Benadryl")
    else:
        print("Recommended: Claritin, Zyrtec, Allegra, & Benadryl")
    pt_pregnancy_status = input("Is the patient pregnant? ")
    if pt_pregnancy_status == "yes":
        print("Recommended allergy medication for pregnant women would be Claritin.")
    else:
        break


allergy_otc()

3 个答案:

答案 0 :(得分:3)

in 运算符检查序列中是否存在对象。所以:

allergy_symptoms = (
    "runny nose",
    "itchy throat",
    "watery eyes",
    "itchy nose"
 )
allergic_reactions = (
    "hives",
    "rash",
    "swelling"
)
severe_reactions = (
    "trouble breathing",
    "throat closing"
)

if user_input in allergy_symptoms:
    print("allergy symptom")
elif user_input in allergic_reactions:
    print("this is an allergic reaction")
elif user_input in severe_reactions:
    print("this is a severe allergic reaction. Call 911!")
    break
else:
    print("not an allergy symptom.")

答案 1 :(得分:0)

将症状归为一组,将反应归为另一组,然后使用 if x in list(如果列表中存在 x)

symptoms = ["runny nose", "itchy throat", "watery eyes"]

user_input = input()

if user_input in symptoms:
    print("you are gonna die")
else:
    print("don't worry, be happy")




答案 2 :(得分:0)

如果您对术语执行查找并返回值,使用字典会更方便。试试这个:

# You should generally avoid "magic values" by declaring them up-front. This
# prevents hard-to-find typing errors
SEVERE_REACTION = "this is a severe allergic reaction. Call 911!"

# define the dictionary outside the get_diagnosis function to avoid 
# re-calculating it every time
symptoms = {
    "runny nose": "allergy symptom",
    "itchy throat": "allergy symptom",
    "watery eyes": "allergy symptom",
    "itchy nose": "allergy symptom",
    "trouble breathing": SEVERE_REACTION,
    "hives": "this is an allergic reaction",
    "rash": "this is an allergic reaction",
    "throat closing": SEVERE_REACTION,
    "swelling": "this is an allergic reaction" }

def get_diagnosis(user_input):
    try:
        return symptoms[user_input]
    # if the user_input isn't in the dictionary, catch the exception and return
    # a message
    except KeyError:
        return "not an allergy symptom."

def symptoms(allergies):
while True:
    user_input = input("enter symptom: ")
    
    diagnosis = get_diagnosis(user_input)
    print(diagnosis)

    if diagnosis == SEVERE_REACTION:
        break