Python 有没有办法缩短if语句?
我让用户输入一个过敏症状,告诉用户该症状是过敏症状,如果症状在元组中,如果不是,那么它将告诉用户不是过敏症状。我曾尝试使用一个元组,并编写了if user\u input==tuple\u name:,但它不起作用。有办法做到这一点吗?如果元组不起作用,有没有办法缩短If-else语句 这是迄今为止的一个小程序:Python 有没有办法缩短if语句?,python,function,loops,if-statement,Python,Function,Loops,If Statement,我让用户输入一个过敏症状,告诉用户该症状是过敏症状,如果症状在元组中,如果不是,那么它将告诉用户不是过敏症状。我曾尝试使用一个元组,并编写了if user\u input==tuple\u name:,但它不起作用。有办法做到这一点吗?如果元组不起作用,有没有办法缩短If-else语句 这是迄今为止的一个小程序: # most common questions asked by patients at pharmacy # allergy treatment def symptoms(aller
# 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运算符检查序列中是否存在对象。因此:
过敏症状=
流鼻涕,
喉咙痒,
水汪汪的眼睛,
鼻痒
过敏反应=
蜂箱,
皮疹
肿胀 的
严重不良反应=
呼吸困难,
咽喉闭合
如果用户输入过敏症状:
过敏症状
过敏反应中的elif用户输入:
这是过敏反应
严重不良反应中的elif用户输入:
这是一种严重的过敏反应。打911!
打破
其他:
这不是过敏症状。
in运算符检查序列中是否存在对象。因此:
过敏症状=
流鼻涕,
喉咙痒,
水汪汪的眼睛,
鼻痒
过敏反应=
蜂箱,
皮疹
肿胀 的
严重不良反应=
呼吸困难,
咽喉闭合
如果用户输入过敏症状:
过敏症状
过敏反应中的elif用户输入:
这是过敏反应
严重不良反应中的elif用户输入:
这是一种严重的过敏反应。打911!
打破
其他:
这不是过敏症状。
将一个列表中的症状和另一个列表中的反应分组,然后如果列表中存在x,则使用列表中的if x 症状=[流鼻涕,喉咙发痒,眼睛流泪] 用户输入=输入 如果用户输入症状: 你会死的 其他: 别担心,开心点
将一个列表中的症状和另一个列表中的反应分组,然后如果列表中存在x,则使用列表中的if x 症状=[流鼻涕,喉咙发痒,眼睛流泪] 用户输入=输入 如果用户输入症状: 你会死的 其他: 别担心,开心点
在对术语执行查找并返回值的情况下,使用字典更方便。试试这个:
# 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
在对术语执行查找并返回值的情况下,使用字典更方便。试试这个:
# 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
如果isinstanceuser_input,tuple:检查输入是否为tuple类型。您正在查找switch或case语句。好消息是,在3.10版本中会有它-,到那时更好的方法是使用字典。请参阅isinstanceuser_input,tuple:以检查输入是否为元组类型。您正在查找switch或case语句。好消息是,在3.10版本中会有它-,到那时更好的方法是使用字典。参考