【问题标题】:Is there a way to make the if statements shorter?有没有办法使 if 语句更短?
【发布时间】:2021-04-17 03:20:19
【问题描述】:

我让用户输入过敏症状并告诉用户该症状是过敏症状,如果症状在元组中,如果不是,那么它会告诉他们“不是过敏症状。我试过了使用元组并写了“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()

【问题讨论】:

标签: python function loops if-statement


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    在一个列表中分组症状,在另一个列表中分组反应,然后使用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")
    
    
    
    
    

    【讨论】:

      【解决方案3】:

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

      # 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-30
        • 2020-11-17
        • 1970-01-01
        • 2019-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-19
        相关资源
        最近更新 更多