Ines Valdovinos
Ines Valdovinos

Reputation: 1

Is there a way to make the if statements shorter?

I am having the user input an allergy symptom and tell the user that the symptom is an allergy symptom, if the symptom is in the tuple and if its not then it'll tell them "not an allergy symptom. I had tried using a tuple and wrote "if user_input == "tuple_name":", but it didn't work. Is there a way to do this? If the tuple doesn't work, is there a way to make the if else statements shorter?

This is the tiny program so far:

# 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()

Upvotes: 0

Views: 61

Answers (3)

Richard Wattenbarger
Richard Wattenbarger

Reputation: 11

In cases where you're performing a lookup on a term and returning a value, it's more convenient to use a dictionary. Try this:

# 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

Upvotes: 0

SirHectorin
SirHectorin

Reputation: 197

group symptoms in one list and reactions in another, then use if x in list (if x is present in list)

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




Upvotes: 0

Cubed
Cubed

Reputation: 102

The in operator checks whether an object exists in a sequence. So:

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

Upvotes: 3

Related Questions