[英]Is there a way to make the if statements shorter?
我讓用戶輸入過敏症狀並告訴用戶該症狀是過敏症狀,如果症狀在元組中,如果不是,那么它會告訴他們“不是過敏症狀。我曾嘗試使用元組並寫了“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()
in
運算符檢查序列中是否存在 object。 所以:
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.")
將症狀分組在一個列表中,將反應分組在另一個列表中,然后使用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")
如果您要查找一個術語並返回一個值,使用字典會更方便。 嘗試這個:
# 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.