【发布时间】: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()
【问题讨论】:
-
if isinstance(user_input, tuple):检查输入是否为元组类型。 -
您正在寻找 switch 或 case 语句。好消息是在 3.10 中会有它 - docs.python.org/3.10/tutorial/controlflow.html#match-statements ,在那之前更好的方法是使用字典。参考stackoverflow.com/questions/60208/…
标签: python function loops if-statement