如何修改我的代码,使它能够以最少的问题得到正确的诊断?

2024-03-29 13:51:29 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是我的完整代码,其中包含了描述每个部分应该操作的注释。在图片中,我提供了它显示了如何根据用户输入“y”表示“是”和“n”表示“否”来验证每个条件。 问题是,我应该只问最小的问题,以便在不回答任何其他问题的情况下得到确切的诊断。在

不要发烧,也不要鼻塞。在

它应该打印:您是疑病患者,只需输入“否”表示发烧,输入“否”表示鼻塞。
我得把整个问卷都看一遍才能显示诊断结果,但这不应该。 我怎样才能修改我的代码,只问最少的问题?在

啊![#说明:创建医疗诊断程序 #询问用户是否发烧,皮疹, #鼻塞如果他们的耳朵痛。在

#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]

Tags: andtheyouinputhavedoprintelif
3条回答

只需根据哪一个问题最具歧视性来构建一个问题的层次结构。你的情况很简单,因为症状完全不连贯。发烧是最重要的问题:如果你没有发烧,你只想知道有没有鼻塞,如果有,在问耳朵之前你想知道有没有皮疹。所以,我会这样开始:

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

既然这看起来像是家庭作业,剩下的就交给你了

这是一个古老的“猜猜谁”游戏,也就是所谓的二元决策树。在

这里不是做作业而是另一个例子

                        male?
                /                   \
               N                     Y
            blonde?                beard?
           /      \               /      \
          N        Y              N       Y
        Sam      Jane            hat?    Andy
                               /     \
                              N       Y
                             Bob     Fred

就解决这些问题而言,OO方法几乎总是最好的,因为它很容易理解,而且添加额外的项目也很容易。使用递归方法来获得答案。。。在

^{pr2}$

好吧,尤其是如果你想扩展这个程序,我建议你使用如下方法:

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。在

相关问题 更多 >