下面是我的完整代码,其中包含了描述每个部分应该操作的注释。在图片中,我提供了它显示了如何根据用户输入“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]
只需根据哪一个问题最具歧视性来构建一个问题的层次结构。你的情况很简单,因为症状完全不连贯。发烧是最重要的问题:如果你没有发烧,你只想知道有没有鼻塞,如果有,在问耳朵之前你想知道有没有皮疹。所以,我会这样开始:
既然这看起来像是家庭作业,剩下的就交给你了
这是一个古老的“猜猜谁”游戏,也就是所谓的二元决策树。在
这里不是做作业而是另一个例子
就解决这些问题而言,OO方法几乎总是最好的,因为它很容易理解,而且添加额外的项目也很容易。使用递归方法来获得答案。。。在
^{pr2}$好吧,尤其是如果你想扩展这个程序,我建议你使用如下方法:
基本上,将您需要的所有函数放入队列中,并返回要跳过的函数数,如果完成了返回-1。在
相关问题 更多 >
编程相关推荐