Phone Tree

Chris Angelico rosuav at
Sun Sep 13 16:59:55 CEST 2015

On Mon, Sep 14, 2015 at 12:39 AM, Azureaus <ltoshea at> wrote:
> The first task is to implement a 'troubleshooting program' similar to what a phone operator would have when you phone up with a problem. We all know the type when you phone up your ISP, 'have you turned it off and on again?', "have you changed your telephone filter' etc..
> It states there should be at least 10 questions and that the user should reach a solution, e.g. 'replace your power cable'. There and my initial reaction was that this could be achieved by lots of if/else statements with each question running onto another one, ugly but it would certainly work. One of them pointed out how inefficient this was and asked if there was another way, they hated writing out tons of if/elif/else statements.
> Does anyone have any ideas for a more elegant solution? My thoughts are that I could use a tree data structure and hence make traversing the tree recursive based on yes or no answers. I'm happy to put the time in to explain these more complex ideas, I'm just hoping those with more expertise than myself could either help verify the idea or suggest alternatives.

This sounds broadly similar to a Twenty Questions game. You may be
able to find some neat implementations of that, which could be
reworked into what you want.

A more cynical algorithm could be implemented as follows:

import random
questions = [
    # The IT Crowd
    "Hello, IT, have you tried turning it off and on again?",
    # The Bastard Operator From Hell
    "How long has this been happening?",
    "Has anyone else had this problem?",
    "Is it your floor that has the Gas Leak?",
    # Standard ISP questions
    "Have you tried disabling your antivirus?",
    "Are we talking to the authorized account holder?",
    # Add additional questions as required
questions = questions[::2] # Ask about half of them
for q in questions:
    response = input(q) # raw_input in Py2
    # Utterly ignore the response
print("I'm sorry, nobody else has reported this problem, so it cannot
be a real problem.")

I may or may not be basing this on real life experience.

For something a bit more useful, though, what you might have is a
nested pile of tuples. For instance:

questions = (
    # First the question
    "Have you rebooted your computer?",
    # Then the if-no response
    "Please reboot your computer, then call back.",
    # Finally the if-yes response
    ("Are you running Windows?",
        ("Are you using a Macintosh?",
            "I'm sorry, we only support Windows and Mac OS.",
            "Let me transfer you to our Macintosh support people.",
        ("Have you disabled your antivirus?",
            "Please disable your AV and try again.",
            ("Is it your floor that has the gas leak?",
                "I'm sorry, I can't help you.",
                "Strike a match. All your problems will disappear.",

while isinstance(questions, tuple):
    q, no, yes = questions
    response = input(q)
    if response == "yes": questions = yes
    elif response == "no": questions = no
    else: print("I'm sorry, I didn't understand that response.")
print(questions) # is now the solution

This is a data-driven algorithm. The code is extremely short, and is
fundamentally about the UI; you could change it completely (a GUI app,
text-to-speech and DTMF for IVR, etc, etc) without changing the
question structure at all.

Is this the kind of tree structure you had in mind?


More information about the Python-list mailing list