下面是我的完整代码,其中包含描述每个部分应该运行的内容的注释。在图片中,我已经提供了它,它显示了如何验证每个条件,具体取决于用户输入'y'表示是和'n'表示没有相关症状。 我遇到的问题是我应该只询问最小的问题,以便在不必回答任何其他问题的情况下获得确切条件的诊断。
实施例。不发烧,也没有鼻塞:Hypochondriac。
它应该打印:你是Hypochondriac,只需为发烧输入no,不输入Stuffy Nose。 我必须通过整个调查问卷来显示诊断,但不应该这样。 如何修改我的代码只询问所需的最小问题?
![#描述:Creata医疗诊断程序 #告诉用户他们是否发烧,皮疹, #a鼻塞,如果他们的耳朵受伤了。
#Get user inputs on whether they have specific conditions
userFever = input("Do you have a fever (y/n): ")
userRash = input("Do you have a rash (y/n): ")
userEar = input("Does your ear hurt (y/n): ")
userNose = input("Do you have a stuffy nose (y/n): ")
#Conditional statements that determine the diagnosis of the user
if userFever == 'n' and userNose == 'n':
print("Diagnosis: You are Hypchondriac")
elif userFever == 'n' and userNose == 'y':
print("Diagnosis: You have a Head Cold")
elif userFever == 'y' and userRash == 'n' and userEar == 'y':
print("Diagnosis: You have an ear infection")
elif userFever == 'y' and userRash == 'n' and userEar == 'n':
print("Diagnosis: You have the flu")
elif userFever == 'y' and userRash == 'y':
print("Diagnosis: You have the measles")][1]
答案 0 :(得分:1)
根据哪个问题最具歧视性,构建一个问题层次结构。在你的情况下很容易,因为症状是完全不相交的。发烧是最重要的问题:如果你不发烧,你只会对是否有鼻塞或不感兴趣。如果有,你想知道在要求耳朵之前是否有皮疹。所以,我就是这样开始的:
userFever = input("Do you have a fever (y/n): ")
if userFever == 'n':
userNose = input("Do you have a stuffy nose (y/n): ")
if userNose == 'n':
...
else:
...
else:
# The other questions
鉴于这看起来像是家庭作业,我把剩下的留给你:P
答案 1 :(得分:0)
特别是如果你想扩展这个程序我建议使用这样的东西:
from itertools import islice
class Diagnosis:
diagnoses = ("You are Hypchondriac", "You have a Head Cold",
"You have an ear infection", "You have the flu", "You have the measles"
)
def __init__(self):
self.diagnosis = 0 # Consider using an enum for this
self.queue = (self.check_fever, self.check_nose, self.check_rash,
self.check_ear)
def ask(self, question):
answer = input(question + " [y/N] ").lower()
return answer != "" and answer[0] == "y"
def make(self):
queue_iter = iter(self.queue)
for func in queue_iter:
skip = func()
# return -1 if you want to break the queue
if skip == -1:
break
if skip > 0:
next(islice(queue_iter, skip, skip + 1))
def check_fever(self):
return 1 if self.ask("Do you have a fever?") else 0
def check_nose(self):
if self.ask("Do you have a stuffy nose?"):
self.diagnosis = 1
return -1
def check_rash(self):
if self.ask("Do you have a rash?"):
self.diagnosis = 4
return -1
return 0
def check_ear(self):
if self.ask("Does your ear hurt?"):
self.diagnosis = 2
else:
self.diagnosis = 3
return -1
def get_result(self):
return self.diagnoses[self.diagnosis]
if __name__ == "__main__":
diagnosis = Diagnosis()
diagnosis.make()
print("Diagnosis: " + diagnosis.get_result())
基本上将您需要的所有功能放入队列并返回要跳过的功能数,或者如果您已完成则返回-1。
答案 2 :(得分:0)
这是旧的'猜猜谁'游戏 - 也称为二元决策树。
而不是在这里做你的功课是一个不同的例子
male?
/ \
N Y
blonde? beard?
/ \ / \
N Y N Y
Sam Jane hat? Andy
/ \
N Y
Bob Fred
在解决这些问题方面,OO方法几乎总是最好的,因为它易于理解并且容易添加额外的项目。使用递归方法得到答案......
class Question(object):
no = None
yes = None
def __init__(self, question):
self.question = question
def answer(self):
r = raw_input("%s (y/N): " % self.question).lower()
next = self.yes if r == 'y' else self.no
# check if we need to descend to the next question
if hasattr(next, 'answer'):
return next.answer()
# otherwise just return the answer
return next
# create a bunch of questions
male = Question("Are you male?")
blonde = Question("Are you blonde?")
beard = Question("Do you have a beard?")
hat = Question("Are you wearing a hat?")
# hook up all the questions according to your diagram
male.no = blonde
male.yes = beard
blonde.no = "Sam"
blonde.yes = "Jane"
beard.no = hat
beard.yes = "Andy"
hat.no = "Bob"
hat.yes = "Fred"
# start the whole thing rolling
print "You are %s." % male.answer()