Reputation: 25
Below is my complete code with comments describing what each section should operate. In the picture,I have provided it shows how each condition would be verified with depending on the users inputting 'y' for yes and 'n' for no concerning symptoms. The problem, I'm having is that I should be only asking the minimal question, in order to get the diagnosis of the exact condition without having to answer any other questions.
Ex. Don't have a fever and don't have a stuffy nose: Hypochondriac.
It should print: You are Hypochondriac, just by inputting no for Fever and no for a Stuffy Nose.
I have to go through the entire questionnaire to display the diagnosis but that shouldn't be.
How can i modify my code to only ask the minimal questions required?
![#Description: Creata a medical diagnosis program #that asks the user whether they have a fever, a rash, #a stuffy nose and if their ear hurts.
#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]
Upvotes: 0
Views: 1233
Reputation: 162
This is the old 'Guess who' game - otherwise known as a binary decision tree.
Rather than do your homework here is a different example
male?
/ \
N Y
blonde? beard?
/ \ / \
N Y N Y
Sam Jane hat? Andy
/ \
N Y
Bob Fred
In terms of solving these then an OO approach is almost always best as it is easily understandable and adding extra items easy. Use a recursive method to get the answer...
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()
Upvotes: 0
Reputation: 9212
Well especially if you want to extend this program I would recommend using something like this:
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())
Basically put all functions you need in the queue and return the number of functions you want to skip or return -1 if you're finished.
Upvotes: 0
Reputation: 9172
Just construct a hierarchy of questions depending on which one discriminates most. In your case is very easy because the symptoms are totally disjoint. The fever would be the top question: if you don't have fever, you're only interested in knowing if there's stuffy nose or no. If there is, you want to know if there's a rash before asking for the ear. So, I'd start like this:
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
Given that this looks like homework, I leave the rest to you :P
Upvotes: 1